<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

<title>module JSON - json: Ruby Standard Library Documentation</title>


<script src="./js/navigation.js" defer></script>
<script src="./js/search.js" defer></script>
<script src="./js/search_index.js" defer></script>
<script src="./js/searcher.js" defer></script>
<script src="./js/darkfish.js" defer></script>

<script src="./js/jquery-3.2.0.min.js"></script>

<script src="./js/vue.min.js"></script>
<script src="./js/js.cookie.min.js"></script>

<link href="./css/fonts.css" rel="stylesheet">
<link id='rdoccss' href="./css/rdoc.css" rel="stylesheet">
<link href="./css/carbon17.css" rel="stylesheet">

<script type="text/javascript">
  var rdoc_rel_prefix = "./";
  var index_rel_prefix = "./";
  var darkModeCsseHref = "./css/rdoc-dm.css"
  var defaultModeCssHref = "./css/rdoc.css"
  // var cssDarkmode = Cookies.get('darkmode');
  
  if( Cookies.get("darkmode") == "true") {
	$('#rdoccss').attr("href", darkModeCsseHref);
}

//  https://cssdeck.com/blog/simple-jquery-stylesheet-switcher/

document.write('<style type="text/css">body{display:none}</style>');

</script>


</head>
<body id="top" role="document" class="module">
  <!-- this is class.html -->

  <div id='actionbar' >
    <div class='wrapper mdiv'>
      <ul class='grids g0'></ul>
    </div> 
    <!-- VERSION HEADER for 3.3.0.preview2 NOT FOUND -->
  </div> <!-- end action bar -->

  <div class='wrapper hdiv'>

    


    <nav id='vapp' role="navigation">
    <div id="project-navigation">
      <div id="home-section" role="region" title="Quick navigation" class="nav-section">
  <h2><a href="./index.html" rel="home">Home</a></h2>

  <div id="table-of-contents-navigation"  >
    <a href="./table_of_contents.html#pages">Pages</a>
    <a href="./table_of_contents.html#classes">Classes</a>
    <a href="./table_of_contents.html#methods">Methods</a>
  </div>
</div>

      <div id="search-section" role="search" class="project-section initially-hidden">
  <form action="#" method="get" accept-charset="utf-8">
    <div id="search-field-wrapper">
      <input id="search-field" role="combobox" aria-label="Search"
             aria-autocomplete="list" aria-controls="search-results"
             type="text" name="search" placeholder="Search" spellcheck="false"
             title="Type to search, Up and Down to navigate, Enter to load">
    </div>

    <ul id="search-results" aria-label="Search Results"
        aria-busy="false" aria-expanded="false"
        aria-atomic="false" class="initially-hidden"></ul>
  </form>
</div>

    </div>


    
<div class="nav-section">
  <h3>Table of Contents</h3>

  <ul class="link-list" role="directory">
    <li><a href="#module-JSON-label-JavaScript+Object+Notation+-28JSON-29">JavaScript Object Notation (JSON)</a>
    <li><a href="#module-JSON-label-Using+Module+JSON">Using Module JSON</a>
    <li><a href="#module-JSON-label-Parsing+JSON">Parsing JSON</a>
    <li><a href="#module-JSON-label-Parsing+JSON+Arrays">Parsing JSON Arrays</a>
    <li><a href="#module-JSON-label-Parsing+JSON+Objects">Parsing JSON Objects</a>
    <li><a href="#module-JSON-label-Parsing+JSON+Scalars">Parsing JSON Scalars</a>
    <li><a href="#module-JSON-label-Parsing+Options">Parsing Options</a>
    <li><a href="#module-JSON-label-Input+Options">Input Options</a>
    <li><a href="#module-JSON-label-Output+Options">Output Options</a>
    <li><a href="#module-JSON-label-Generating+JSON">Generating JSON</a>
    <li><a href="#module-JSON-label-Generating+JSON+from+Arrays">Generating JSON from Arrays</a>
    <li><a href="#module-JSON-label-Generating+JSON+from+Hashes">Generating JSON from Hashes</a>
    <li><a href="#module-JSON-label-Generating+JSON+from+Other+Objects">Generating JSON from Other Objects</a>
    <li><a href="#module-JSON-label-Generating+Options">Generating Options</a>
    <li><a href="#module-JSON-label-Input+Options">Input Options</a>
    <li><a href="#module-JSON-label-Output+Options">Output Options</a>
    <li><a href="#module-JSON-label-JSON+Additions">JSON Additions</a>
    <li><a href="#module-JSON-label-Built-in+Additions">Built-in Additions</a>
    <li><a href="#module-JSON-label-Custom+JSON+Additions">Custom JSON Additions</a>
  </ul>
</div>


    <button id='toggleThing' @click="toggleNav()" >Show/hide navigation</button>
    <div :class="isOpen ? 'block' : 'hidden' " id='toggleMe'>
      <div id="class-metadata">
        
        
        
        
        
<!-- Method Quickref -->
<div id="method-list-section" class="nav-section">
  <h3>Methods</h3>

  <ul class="link-list" role="directory">
    <li ><a href="#method-c-5B-5D">::[]</a>
    <li ><a href="#method-c-create_fast_state">::create_fast_state</a>
    <li ><a href="#method-c-create_id">::create_id</a>
    <li ><a href="#method-c-create_id-3D">::create_id=</a>
    <li ><a href="#method-c-create_pretty_state">::create_pretty_state</a>
    <li ><a href="#method-c-iconv">::iconv</a>
    <li ><a href="#method-c-restore">::restore</a>
    <li ><a href="#method-i-dump">#dump</a>
    <li ><a href="#method-i-fast_generate">#fast_generate</a>
    <li ><a href="#method-i-generate">#generate</a>
    <li ><a href="#method-i-load">#load</a>
    <li ><a href="#method-i-load_file">#load_file</a>
    <li ><a href="#method-i-load_file-21">#load_file!</a>
    <li ><a href="#method-i-parse">#parse</a>
    <li ><a href="#method-i-parse-21">#parse!</a>
    <li ><a href="#method-i-pretty_generate">#pretty_generate</a>
    <li ><a href="#method-i-restore">#restore</a>
  </ul>
</div>

      </div>
     </div>
    </nav>


    <div id='extraz'><div class='adzbox-index'  >
      
     </div>         
    </div>

    <main role="main" aria-labelledby="module-JSON">
    <h1 id="module-JSON" class="module">
      module JSON
    </h1>

    <section class="description">
    
<h1 id="module-JSON-label-JavaScript+Object+Notation+-28JSON-29">JavaScript Object Notation (JSON)<span><a href="#module-JSON-label-JavaScript+Object+Notation+-28JSON-29">&para;</a> <a href="#top">&uarr;</a></span></h1>

<p>JSON is a lightweight data-interchange format.</p>

<p>A JSON value is one of the following:</p>
<ul><li>
<p>Double-quoted text:  <code>&quot;foo&quot;</code>.</p>
</li><li>
<p>Number:  <code>1</code>, <code>1.0</code>, <code>2.0e2</code>.</p>
</li><li>
<p>Boolean:  <code>true</code>, <code>false</code>.</p>
</li><li>
<p>Null: <code>null</code>.</p>
</li><li>
<p>Array: an ordered list of values, enclosed by square brackets:</p>

<pre class="ruby">[<span class="ruby-string">&quot;foo&quot;</span>, <span class="ruby-value">1</span>, <span class="ruby-value">1.0</span>, <span class="ruby-value">2.0e2</span>, <span class="ruby-keyword">true</span>, <span class="ruby-keyword">false</span>, <span class="ruby-identifier">null</span>]
</pre>
</li><li>
<p>Object: a collection of name/value pairs, enclosed by curly braces; each name is double-quoted text; the values may be any JSON values:</p>

<pre class="ruby">{<span class="ruby-value">&quot;a&quot;:</span> <span class="ruby-string">&quot;foo&quot;</span>, <span class="ruby-value">&quot;b&quot;:</span> <span class="ruby-value">1</span>, <span class="ruby-value">&quot;c&quot;:</span> <span class="ruby-value">1.0</span>, <span class="ruby-value">&quot;d&quot;:</span> <span class="ruby-value">2.0e2</span>, <span class="ruby-value">&quot;e&quot;:</span> <span class="ruby-keyword">true</span>, <span class="ruby-value">&quot;f&quot;:</span> <span class="ruby-keyword">false</span>, <span class="ruby-value">&quot;g&quot;:</span> <span class="ruby-identifier">null</span>}
</pre>
</li></ul>

<p>A JSON array or object may contain nested arrays, objects, and scalars to any depth:</p>

<pre class="ruby">{<span class="ruby-value">&quot;foo&quot;:</span> {<span class="ruby-value">&quot;bar&quot;:</span> <span class="ruby-value">1</span>, <span class="ruby-value">&quot;baz&quot;:</span> <span class="ruby-value">2</span>}, <span class="ruby-value">&quot;bat&quot;:</span> [<span class="ruby-value">0</span>, <span class="ruby-value">1</span>, <span class="ruby-value">2</span>]}
[{<span class="ruby-value">&quot;foo&quot;:</span> <span class="ruby-value">0</span>, <span class="ruby-value">&quot;bar&quot;:</span> <span class="ruby-value">1</span>}, [<span class="ruby-string">&quot;baz&quot;</span>, <span class="ruby-value">2</span>]]
</pre>

<h2 id="module-JSON-label-Using+Module+JSON">Using Module JSON<span><a href="#module-JSON-label-Using+Module+JSON">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>To make module JSON available in your code, begin with:</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;json&#39;</span>
</pre>

<p>All examples here assume that this has been done.</p>

<h3 id="module-JSON-label-Parsing+JSON">Parsing JSON<span><a href="#module-JSON-label-Parsing+JSON">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>You can parse a String containing JSON data using either of two methods:</p>
<ul><li>
<p><code>JSON.parse(source, opts)</code></p>
</li><li>
<p><code>JSON.parse!(source, opts)</code></p>
</li></ul>

<p>where</p>
<ul><li>
<p><code>source</code> is a Ruby object.</p>
</li><li>
<p><code>opts</code> is a Hash object containing options that control both input allowed and output formatting.</p>
</li></ul>

<p>The difference between the two methods is that <a href="JSON.html#method-i-parse-21"><code>JSON.parse!</code></a> omits some checks and may not be safe for some <code>source</code> data; use it only for data from trusted sources. Use the safer method <a href="JSON.html#method-i-parse"><code>JSON.parse</code></a> for less trusted sources.</p>

<h4 id="module-JSON-label-Parsing+JSON+Arrays">Parsing JSON Arrays<span><a href="#module-JSON-label-Parsing+JSON+Arrays">&para;</a> <a href="#top">&uarr;</a></span></h4>

<p>When <code>source</code> is a JSON array, <a href="JSON.html#method-i-parse"><code>JSON.parse</code></a> by default returns a Ruby Array:</p>

<pre class="ruby"><span class="ruby-identifier">json</span> = <span class="ruby-string">&#39;[&quot;foo&quot;, 1, 1.0, 2.0e2, true, false, null]&#39;</span>
<span class="ruby-identifier">ruby</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">json</span>)
<span class="ruby-identifier">ruby</span> <span class="ruby-comment"># =&gt; [&quot;foo&quot;, 1, 1.0, 200.0, true, false, nil]</span>
<span class="ruby-identifier">ruby</span>.<span class="ruby-identifier">class</span> <span class="ruby-comment"># =&gt; Array</span>
</pre>

<p>The JSON array may contain nested arrays, objects, and scalars to any depth:</p>

<pre class="ruby"><span class="ruby-identifier">json</span> = <span class="ruby-string">&#39;[{&quot;foo&quot;: 0, &quot;bar&quot;: 1}, [&quot;baz&quot;, 2]]&#39;</span>
<span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">json</span>) <span class="ruby-comment"># =&gt; [{&quot;foo&quot;=&gt;0, &quot;bar&quot;=&gt;1}, [&quot;baz&quot;, 2]]</span>
</pre>

<h4 id="module-JSON-label-Parsing+JSON+Objects">Parsing JSON Objects<span><a href="#module-JSON-label-Parsing+JSON+Objects">&para;</a> <a href="#top">&uarr;</a></span></h4>

<p>When the source is a JSON object, <a href="JSON.html#method-i-parse"><code>JSON.parse</code></a> by default returns a Ruby Hash:</p>

<pre class="ruby"><span class="ruby-identifier">json</span> = <span class="ruby-string">&#39;{&quot;a&quot;: &quot;foo&quot;, &quot;b&quot;: 1, &quot;c&quot;: 1.0, &quot;d&quot;: 2.0e2, &quot;e&quot;: true, &quot;f&quot;: false, &quot;g&quot;: null}&#39;</span>
<span class="ruby-identifier">ruby</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">json</span>)
<span class="ruby-identifier">ruby</span> <span class="ruby-comment"># =&gt; {&quot;a&quot;=&gt;&quot;foo&quot;, &quot;b&quot;=&gt;1, &quot;c&quot;=&gt;1.0, &quot;d&quot;=&gt;200.0, &quot;e&quot;=&gt;true, &quot;f&quot;=&gt;false, &quot;g&quot;=&gt;nil}</span>
<span class="ruby-identifier">ruby</span>.<span class="ruby-identifier">class</span> <span class="ruby-comment"># =&gt; Hash</span>
</pre>

<p>The JSON object may contain nested arrays, objects, and scalars to any depth:</p>

<pre class="ruby"><span class="ruby-identifier">json</span> = <span class="ruby-string">&#39;{&quot;foo&quot;: {&quot;bar&quot;: 1, &quot;baz&quot;: 2}, &quot;bat&quot;: [0, 1, 2]}&#39;</span>
<span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">json</span>) <span class="ruby-comment"># =&gt; {&quot;foo&quot;=&gt;{&quot;bar&quot;=&gt;1, &quot;baz&quot;=&gt;2}, &quot;bat&quot;=&gt;[0, 1, 2]}</span>
</pre>

<h4 id="module-JSON-label-Parsing+JSON+Scalars">Parsing JSON Scalars<span><a href="#module-JSON-label-Parsing+JSON+Scalars">&para;</a> <a href="#top">&uarr;</a></span></h4>

<p>When the source is a JSON scalar (not an array or object), <a href="JSON.html#method-i-parse"><code>JSON.parse</code></a> returns a Ruby scalar.</p>

<p>String:</p>

<pre class="ruby"><span class="ruby-identifier">ruby</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-string">&#39;&quot;foo&quot;&#39;</span>)
<span class="ruby-identifier">ruby</span> <span class="ruby-comment"># =&gt; &#39;foo&#39;</span>
<span class="ruby-identifier">ruby</span>.<span class="ruby-identifier">class</span> <span class="ruby-comment"># =&gt; String</span>
</pre>

<p>Integer:</p>

<pre class="ruby"><span class="ruby-identifier">ruby</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-string">&#39;1&#39;</span>)
<span class="ruby-identifier">ruby</span> <span class="ruby-comment"># =&gt; 1</span>
<span class="ruby-identifier">ruby</span>.<span class="ruby-identifier">class</span> <span class="ruby-comment"># =&gt; Integer</span>
</pre>

<p>Float:</p>

<pre class="ruby"><span class="ruby-identifier">ruby</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-string">&#39;1.0&#39;</span>)
<span class="ruby-identifier">ruby</span> <span class="ruby-comment"># =&gt; 1.0</span>
<span class="ruby-identifier">ruby</span>.<span class="ruby-identifier">class</span> <span class="ruby-comment"># =&gt; Float</span>
<span class="ruby-identifier">ruby</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-string">&#39;2.0e2&#39;</span>)
<span class="ruby-identifier">ruby</span> <span class="ruby-comment"># =&gt; 200</span>
<span class="ruby-identifier">ruby</span>.<span class="ruby-identifier">class</span> <span class="ruby-comment"># =&gt; Float</span>
</pre>

<p>Boolean:</p>

<pre class="ruby"><span class="ruby-identifier">ruby</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-string">&#39;true&#39;</span>)
<span class="ruby-identifier">ruby</span> <span class="ruby-comment"># =&gt; true</span>
<span class="ruby-identifier">ruby</span>.<span class="ruby-identifier">class</span> <span class="ruby-comment"># =&gt; TrueClass</span>
<span class="ruby-identifier">ruby</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-string">&#39;false&#39;</span>)
<span class="ruby-identifier">ruby</span> <span class="ruby-comment"># =&gt; false</span>
<span class="ruby-identifier">ruby</span>.<span class="ruby-identifier">class</span> <span class="ruby-comment"># =&gt; FalseClass</span>
</pre>

<p>Null:</p>

<pre class="ruby"><span class="ruby-identifier">ruby</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-string">&#39;null&#39;</span>)
<span class="ruby-identifier">ruby</span> <span class="ruby-comment"># =&gt; nil</span>
<span class="ruby-identifier">ruby</span>.<span class="ruby-identifier">class</span> <span class="ruby-comment"># =&gt; NilClass</span>
</pre>

<h4 id="module-JSON-label-Parsing+Options">Parsing Options<span><a href="#module-JSON-label-Parsing+Options">&para;</a> <a href="#top">&uarr;</a></span></h4>

<h6 id="module-JSON-label-Input+Options">Input Options<span><a href="#module-JSON-label-Input+Options">&para;</a> <a href="#top">&uarr;</a></span></h6>

<p>Option <code>max_nesting</code> (Integer) specifies the maximum nesting depth allowed; defaults to <code>100</code>; specify <code>false</code> to disable depth checking.</p>

<p>With the default, <code>false</code>:</p>

<pre class="ruby"><span class="ruby-identifier">source</span> = <span class="ruby-string">&#39;[0, [1, [2, [3]]]]&#39;</span>
<span class="ruby-identifier">ruby</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">source</span>)
<span class="ruby-identifier">ruby</span> <span class="ruby-comment"># =&gt; [0, [1, [2, [3]]]]</span>
</pre>

<p>Too deep:</p>

<pre class="ruby"><span class="ruby-comment"># Raises JSON::NestingError (nesting of 2 is too deep):</span>
<span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">source</span>, {<span class="ruby-value">max_nesting:</span> <span class="ruby-value">1</span>})
</pre>

<p>Bad value:</p>

<pre class="ruby"><span class="ruby-comment"># Raises TypeError (wrong argument type Symbol (expected Fixnum)):</span>
<span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">source</span>, {<span class="ruby-value">max_nesting:</span> <span class="ruby-value">:foo</span>})
</pre>
<hr>

<p>Option <code>allow_nan</code> (boolean) specifies whether to allow <a href="JSON.html#NaN"><code>NaN</code></a>, <a href="JSON.html#Infinity"><code>Infinity</code></a>, and <a href="JSON.html#MinusInfinity"><code>MinusInfinity</code></a> in <code>source</code>; defaults to <code>false</code>.</p>

<p>With the default, <code>false</code>:</p>

<pre class="ruby"><span class="ruby-comment"># Raises JSON::ParserError (225: unexpected token at &#39;[NaN]&#39;):</span>
<span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-string">&#39;[NaN]&#39;</span>)
<span class="ruby-comment"># Raises JSON::ParserError (232: unexpected token at &#39;[Infinity]&#39;):</span>
<span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-string">&#39;[Infinity]&#39;</span>)
<span class="ruby-comment"># Raises JSON::ParserError (248: unexpected token at &#39;[-Infinity]&#39;):</span>
<span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-string">&#39;[-Infinity]&#39;</span>)
</pre>

<p>Allow:</p>

<pre class="ruby"><span class="ruby-identifier">source</span> = <span class="ruby-string">&#39;[NaN, Infinity, -Infinity]&#39;</span>
<span class="ruby-identifier">ruby</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">source</span>, {<span class="ruby-value">allow_nan:</span> <span class="ruby-keyword">true</span>})
<span class="ruby-identifier">ruby</span> <span class="ruby-comment"># =&gt; [NaN, Infinity, -Infinity]</span>
</pre>

<h6 id="module-JSON-label-Output+Options">Output Options<span><a href="#module-JSON-label-Output+Options">&para;</a> <a href="#top">&uarr;</a></span></h6>

<p>Option <code>symbolize_names</code> (boolean) specifies whether returned Hash keys should be Symbols; defaults to <code>false</code> (use Strings).</p>

<p>With the default, <code>false</code>:</p>

<pre class="ruby"><span class="ruby-identifier">source</span> = <span class="ruby-string">&#39;{&quot;a&quot;: &quot;foo&quot;, &quot;b&quot;: 1.0, &quot;c&quot;: true, &quot;d&quot;: false, &quot;e&quot;: null}&#39;</span>
<span class="ruby-identifier">ruby</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">source</span>)
<span class="ruby-identifier">ruby</span> <span class="ruby-comment"># =&gt; {&quot;a&quot;=&gt;&quot;foo&quot;, &quot;b&quot;=&gt;1.0, &quot;c&quot;=&gt;true, &quot;d&quot;=&gt;false, &quot;e&quot;=&gt;nil}</span>
</pre>

<p>Use Symbols:</p>

<pre class="ruby"><span class="ruby-identifier">ruby</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">source</span>, {<span class="ruby-value">symbolize_names:</span> <span class="ruby-keyword">true</span>})
<span class="ruby-identifier">ruby</span> <span class="ruby-comment"># =&gt; {:a=&gt;&quot;foo&quot;, :b=&gt;1.0, :c=&gt;true, :d=&gt;false, :e=&gt;nil}</span>
</pre>
<hr>

<p>Option <code>object_class</code> (Class) specifies the Ruby class to be used for each JSON object; defaults to Hash.</p>

<p>With the default, Hash:</p>

<pre class="ruby"><span class="ruby-identifier">source</span> = <span class="ruby-string">&#39;{&quot;a&quot;: &quot;foo&quot;, &quot;b&quot;: 1.0, &quot;c&quot;: true, &quot;d&quot;: false, &quot;e&quot;: null}&#39;</span>
<span class="ruby-identifier">ruby</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">source</span>)
<span class="ruby-identifier">ruby</span>.<span class="ruby-identifier">class</span> <span class="ruby-comment"># =&gt; Hash</span>
</pre>

<p>Use class OpenStruct:</p>

<pre class="ruby"><span class="ruby-identifier">ruby</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">source</span>, {<span class="ruby-value">object_class:</span> <span class="ruby-constant">OpenStruct</span>})
<span class="ruby-identifier">ruby</span> <span class="ruby-comment"># =&gt; #&lt;OpenStruct a=&quot;foo&quot;, b=1.0, c=true, d=false, e=nil&gt;</span>
</pre>
<hr>

<p>Option <code>array_class</code> (Class) specifies the Ruby class to be used for each JSON array; defaults to Array.</p>

<p>With the default, Array:</p>

<pre class="ruby"><span class="ruby-identifier">source</span> = <span class="ruby-string">&#39;[&quot;foo&quot;, 1.0, true, false, null]&#39;</span>
<span class="ruby-identifier">ruby</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">source</span>)
<span class="ruby-identifier">ruby</span>.<span class="ruby-identifier">class</span> <span class="ruby-comment"># =&gt; Array</span>
</pre>

<p>Use class Set:</p>

<pre class="ruby"><span class="ruby-identifier">ruby</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">source</span>, {<span class="ruby-value">array_class:</span> <span class="ruby-constant">Set</span>})
<span class="ruby-identifier">ruby</span> <span class="ruby-comment"># =&gt; #&lt;Set: {&quot;foo&quot;, 1.0, true, false, nil}&gt;</span>
</pre>
<hr>

<p>Option <code>create_additions</code> (boolean) specifies whether to use JSON additions in parsing. See <a href="#module-JSON-label-JSON+Additions">JSON Additions</a>.</p>

<h3 id="module-JSON-label-Generating+JSON">Generating JSON<span><a href="#module-JSON-label-Generating+JSON">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>To generate a Ruby String containing JSON data, use method <code>JSON.generate(source, opts)</code>, where</p>
<ul><li>
<p><code>source</code> is a Ruby object.</p>
</li><li>
<p><code>opts</code> is a Hash object containing options that control both input allowed and output formatting.</p>
</li></ul>

<h4 id="module-JSON-label-Generating+JSON+from+Arrays">Generating JSON from Arrays<span><a href="#module-JSON-label-Generating+JSON+from+Arrays">&para;</a> <a href="#top">&uarr;</a></span></h4>

<p>When the source is a Ruby Array, <a href="JSON.html#method-i-generate"><code>JSON.generate</code></a> returns a String containing a JSON array:</p>

<pre class="ruby"><span class="ruby-identifier">ruby</span> = [<span class="ruby-value">0</span>, <span class="ruby-string">&#39;s&#39;</span>, <span class="ruby-value">:foo</span>]
<span class="ruby-identifier">json</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">ruby</span>)
<span class="ruby-identifier">json</span> <span class="ruby-comment"># =&gt; &#39;[0,&quot;s&quot;,&quot;foo&quot;]&#39;</span>
</pre>

<p>The Ruby Array array may contain nested arrays, hashes, and scalars to any depth:</p>

<pre class="ruby"><span class="ruby-identifier">ruby</span> = [<span class="ruby-value">0</span>, [<span class="ruby-value">1</span>, <span class="ruby-value">2</span>], {<span class="ruby-value">foo:</span> <span class="ruby-value">3</span>, <span class="ruby-value">bar:</span> <span class="ruby-value">4</span>}]
<span class="ruby-identifier">json</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">ruby</span>)
<span class="ruby-identifier">json</span> <span class="ruby-comment"># =&gt; &#39;[0,[1,2],{&quot;foo&quot;:3,&quot;bar&quot;:4}]&#39;</span>
</pre>

<h4 id="module-JSON-label-Generating+JSON+from+Hashes">Generating JSON from Hashes<span><a href="#module-JSON-label-Generating+JSON+from+Hashes">&para;</a> <a href="#top">&uarr;</a></span></h4>

<p>When the source is a Ruby Hash, <a href="JSON.html#method-i-generate"><code>JSON.generate</code></a> returns a String containing a JSON object:</p>

<pre class="ruby"><span class="ruby-identifier">ruby</span> = {<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-string">&#39;s&#39;</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">:bat</span>}
<span class="ruby-identifier">json</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">ruby</span>)
<span class="ruby-identifier">json</span> <span class="ruby-comment"># =&gt; &#39;{&quot;foo&quot;:0,&quot;bar&quot;:&quot;s&quot;,&quot;baz&quot;:&quot;bat&quot;}&#39;</span>
</pre>

<p>The Ruby Hash array may contain nested arrays, hashes, and scalars to any depth:</p>

<pre class="ruby"><span class="ruby-identifier">ruby</span> = {<span class="ruby-value">foo:</span> [<span class="ruby-value">0</span>, <span class="ruby-value">1</span>], <span class="ruby-value">bar:</span> {<span class="ruby-value">baz:</span> <span class="ruby-value">2</span>, <span class="ruby-value">bat:</span> <span class="ruby-value">3</span>}, <span class="ruby-value">bam:</span> <span class="ruby-value">:bad</span>}
<span class="ruby-identifier">json</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">ruby</span>)
<span class="ruby-identifier">json</span> <span class="ruby-comment"># =&gt; &#39;{&quot;foo&quot;:[0,1],&quot;bar&quot;:{&quot;baz&quot;:2,&quot;bat&quot;:3},&quot;bam&quot;:&quot;bad&quot;}&#39;</span>
</pre>

<h4 id="module-JSON-label-Generating+JSON+from+Other+Objects">Generating JSON from Other Objects<span><a href="#module-JSON-label-Generating+JSON+from+Other+Objects">&para;</a> <a href="#top">&uarr;</a></span></h4>

<p>When the source is neither an Array nor a Hash, the generated JSON data depends on the class of the source.</p>

<p>When the source is a Ruby Integer or Float, <a href="JSON.html#method-i-generate"><code>JSON.generate</code></a> returns a String containing a JSON number:</p>

<pre class="ruby"><span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-value">42</span>) <span class="ruby-comment"># =&gt; &#39;42&#39;</span>
<span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-value">0.42</span>) <span class="ruby-comment"># =&gt; &#39;0.42&#39;</span>
</pre>

<p>When the source is a Ruby String, <a href="JSON.html#method-i-generate"><code>JSON.generate</code></a> returns a String containing a JSON string (with double-quotes):</p>

<pre class="ruby"><span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-string">&#39;A string&#39;</span>) <span class="ruby-comment"># =&gt; &#39;&quot;A string&quot;&#39;</span>
</pre>

<p>When the source is <code>true</code>, <code>false</code> or <code>nil</code>, <a href="JSON.html#method-i-generate"><code>JSON.generate</code></a> returns a String containing the corresponding JSON token:</p>

<pre class="ruby"><span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-keyword">true</span>) <span class="ruby-comment"># =&gt; &#39;true&#39;</span>
<span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-keyword">false</span>) <span class="ruby-comment"># =&gt; &#39;false&#39;</span>
<span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-keyword">nil</span>) <span class="ruby-comment"># =&gt; &#39;null&#39;</span>
</pre>

<p>When the source is none of the above, <a href="JSON.html#method-i-generate"><code>JSON.generate</code></a> returns a String containing a JSON string representation of the source:</p>

<pre class="ruby"><span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-value">:foo</span>) <span class="ruby-comment"># =&gt; &#39;&quot;foo&quot;&#39;</span>
<span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-constant">Complex</span>(<span class="ruby-value">0</span>, <span class="ruby-value">0</span>)) <span class="ruby-comment"># =&gt; &#39;&quot;0+0i&quot;&#39;</span>
<span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-constant">Dir</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&#39;.&#39;</span>)) <span class="ruby-comment"># =&gt; &#39;&quot;#&lt;Dir&gt;&quot;&#39;</span>
</pre>

<h4 id="module-JSON-label-Generating+Options">Generating Options<span><a href="#module-JSON-label-Generating+Options">&para;</a> <a href="#top">&uarr;</a></span></h4>

<h6 id="module-JSON-label-Input+Options">Input Options<span><a href="#module-JSON-label-Input+Options">&para;</a> <a href="#top">&uarr;</a></span></h6>

<p>Option <code>allow_nan</code> (boolean) specifies whether <code>NaN</code>, <code>Infinity</code>, and <code>-Infinity</code> may be generated; defaults to <code>false</code>.</p>

<p>With the default, <code>false</code>:</p>

<pre class="ruby"><span class="ruby-comment"># Raises JSON::GeneratorError (920: NaN not allowed in JSON):</span>
<span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-constant">JSON</span><span class="ruby-operator">::</span><span class="ruby-constant">NaN</span>)
<span class="ruby-comment"># Raises JSON::GeneratorError (917: Infinity not allowed in JSON):</span>
<span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-constant">JSON</span><span class="ruby-operator">::</span><span class="ruby-constant">Infinity</span>)
<span class="ruby-comment"># Raises JSON::GeneratorError (917: -Infinity not allowed in JSON):</span>
<span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-constant">JSON</span><span class="ruby-operator">::</span><span class="ruby-constant">MinusInfinity</span>)
</pre>

<p>Allow:</p>

<pre class="ruby"><span class="ruby-identifier">ruby</span> = [<span class="ruby-constant">Float</span><span class="ruby-operator">::</span><span class="ruby-constant">NaN</span>, <span class="ruby-constant">Float</span><span class="ruby-operator">::</span><span class="ruby-constant">Infinity</span>, <span class="ruby-constant">Float</span><span class="ruby-operator">::</span><span class="ruby-constant">MinusInfinity</span>]
<span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">ruby</span>, <span class="ruby-value">allow_nan:</span> <span class="ruby-keyword">true</span>) <span class="ruby-comment"># =&gt; &#39;[NaN,Infinity,-Infinity]&#39;</span>
</pre>
<hr>

<p>Option <code>max_nesting</code> (Integer) specifies the maximum nesting depth in <code>obj</code>; defaults to <code>100</code>.</p>

<p>With the default, <code>100</code>:</p>

<pre class="ruby"><span class="ruby-identifier">obj</span> = [[[[[[<span class="ruby-value">0</span>]]]]]]
<span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">obj</span>) <span class="ruby-comment"># =&gt; &#39;[[[[[[0]]]]]]&#39;</span>
</pre>

<p>Too deep:</p>

<pre class="ruby"><span class="ruby-comment"># Raises JSON::NestingError (nesting of 2 is too deep):</span>
<span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">obj</span>, <span class="ruby-value">max_nesting:</span> <span class="ruby-value">2</span>)
</pre>

<h6 id="module-JSON-label-Output+Options">Output Options<span><a href="#module-JSON-label-Output+Options">&para;</a> <a href="#top">&uarr;</a></span></h6>

<p>The default formatting options generate the most compact JSON data, all on one line and with no whitespace.</p>

<p>You can use these formatting options to generate JSON data in a more open format, using whitespace. See also <a href="JSON.html#method-i-pretty_generate"><code>JSON.pretty_generate</code></a>.</p>
<ul><li>
<p>Option <code>array_nl</code> (String) specifies a string (usually a newline) to be inserted after each JSON array; defaults to the empty String, <code>&#39;&#39;</code>.</p>
</li><li>
<p>Option <code>object_nl</code> (String) specifies a string (usually a newline) to be inserted after each JSON object; defaults to the empty String, <code>&#39;&#39;</code>.</p>
</li><li>
<p>Option <code>indent</code> (String) specifies the string (usually spaces) to be used for indentation; defaults to the empty String, <code>&#39;&#39;</code>; defaults to the empty String, <code>&#39;&#39;</code>; has no effect unless options <code>array_nl</code> or <code>object_nl</code> specify newlines.</p>
</li><li>
<p>Option <code>space</code> (String) specifies a string (usually a space) to be inserted after the colon in each JSON object’s pair; defaults to the empty String, <code>&#39;&#39;</code>.</p>
</li><li>
<p>Option <code>space_before</code> (String) specifies a string (usually a space) to be inserted before the colon in each JSON object’s pair; defaults to the empty String, <code>&#39;&#39;</code>.</p>
</li></ul>

<p>In this example, <code>obj</code> is used first to generate the shortest JSON data (no whitespace), then again with all formatting options specified:</p>

<pre class="ruby"><span class="ruby-identifier">obj</span> = {<span class="ruby-value">foo:</span> [<span class="ruby-value">:bar</span>, <span class="ruby-value">:baz</span>], <span class="ruby-value">bat:</span> {<span class="ruby-value">bam:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bad:</span> <span class="ruby-value">1</span>}}
<span class="ruby-identifier">json</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">obj</span>)
<span class="ruby-identifier">puts</span> <span class="ruby-string">&#39;Compact:&#39;</span>, <span class="ruby-identifier">json</span>
<span class="ruby-identifier">opts</span> = {
  <span class="ruby-value">array_nl:</span> <span class="ruby-string">&quot;\n&quot;</span>,
  <span class="ruby-value">object_nl:</span> <span class="ruby-string">&quot;\n&quot;</span>,
  <span class="ruby-value">indent:</span> <span class="ruby-string">&#39;  &#39;</span>,
  <span class="ruby-value">space_before:</span> <span class="ruby-string">&#39; &#39;</span>,
  <span class="ruby-value">space:</span> <span class="ruby-string">&#39; &#39;</span>
}
<span class="ruby-identifier">puts</span> <span class="ruby-string">&#39;Open:&#39;</span>, <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">obj</span>, <span class="ruby-identifier">opts</span>)
</pre>

<p>Output:</p>

<pre>Compact:
{&quot;foo&quot;:[&quot;bar&quot;,&quot;baz&quot;],&quot;bat&quot;:{&quot;bam&quot;:0,&quot;bad&quot;:1}}
Open:
{
  &quot;foo&quot; : [
    &quot;bar&quot;,
    &quot;baz&quot;
],
  &quot;bat&quot; : {
    &quot;bam&quot; : 0,
    &quot;bad&quot; : 1
  }
}</pre>

<h2 id="module-JSON-label-JSON+Additions">JSON Additions<span><a href="#module-JSON-label-JSON+Additions">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>When you “round trip” a non-String object from Ruby to JSON and back, you have a new String, instead of the object you began with:</p>

<pre class="ruby"><span class="ruby-identifier">ruby0</span> = <span class="ruby-constant">Range</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value">0</span>, <span class="ruby-value">2</span>)
<span class="ruby-identifier">json</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">ruby0</span>)
<span class="ruby-identifier">json</span> <span class="ruby-comment"># =&gt; &#39;0..2&quot;&#39;</span>
<span class="ruby-identifier">ruby1</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">json</span>)
<span class="ruby-identifier">ruby1</span> <span class="ruby-comment"># =&gt; &#39;0..2&#39;</span>
<span class="ruby-identifier">ruby1</span>.<span class="ruby-identifier">class</span> <span class="ruby-comment"># =&gt; String</span>
</pre>

<p>You can use JSON <em>additions</em> to preserve the original object. The addition is an extension of a ruby class, so that:</p>
<ul><li>
<p>JSON.generate stores more information in the JSON string.</p>
</li><li>
<p>JSON.parse, called with option <code>create_additions</code>, uses that information to create a proper Ruby object.</p>
</li></ul>

<p>This example shows a Range being generated into JSON and parsed back into Ruby, both without and with the addition for Range:</p>

<pre class="ruby"><span class="ruby-identifier">ruby</span> = <span class="ruby-constant">Range</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value">0</span>, <span class="ruby-value">2</span>)
<span class="ruby-comment"># This passage does not use the addition for Range.</span>
<span class="ruby-identifier">json0</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">ruby</span>)
<span class="ruby-identifier">ruby0</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">json0</span>)
<span class="ruby-comment"># This passage uses the addition for Range.</span>
<span class="ruby-identifier">require</span> <span class="ruby-string">&#39;json/add/range&#39;</span>
<span class="ruby-identifier">json1</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">ruby</span>)
<span class="ruby-identifier">ruby1</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">json1</span>, <span class="ruby-value">create_additions:</span> <span class="ruby-keyword">true</span>)
<span class="ruby-comment"># Make a nice display.</span>
<span class="ruby-identifier">display</span> = <span class="ruby-identifier">&lt;&lt;EOT</span>
<span class="ruby-value">Generated JSON:
  Without addition:  #{json0} (#{json0.class})
  With addition:     #{json1} (#{json1.class})
Parsed JSON:
  Without addition:  #{ruby0.inspect} (#{ruby0.class})
  With addition:     #{ruby1.inspect} (#{ruby1.class})
</span><span class="ruby-identifier">EOT</span>
<span class="ruby-identifier">puts</span> <span class="ruby-identifier">display</span>
</pre>

<p>This output shows the different results:</p>

<pre>Generated JSON:
  Without addition:  &quot;0..2&quot; (String)
  With addition:     {&quot;json_class&quot;:&quot;Range&quot;,&quot;a&quot;:[0,2,false]} (String)
Parsed JSON:
  Without addition:  &quot;0..2&quot; (String)
  With addition:     0..2 (Range)</pre>

<p>The JSON module includes additions for certain classes. You can also craft custom additions. See <a href="#module-JSON-label-Custom+JSON+Additions">Custom JSON Additions</a>.</p>

<h3 id="module-JSON-label-Built-in+Additions">Built-in Additions<span><a href="#module-JSON-label-Built-in+Additions">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>The JSON module includes additions for certain classes. To use an addition, <code>require</code> its source:</p>
<ul><li>
<p>BigDecimal: <code>require &#39;json/add/bigdecimal&#39;</code></p>
</li><li>
<p>Complex: <code>require &#39;json/add/complex&#39;</code></p>
</li><li>
<p>Date: <code>require &#39;json/add/date&#39;</code></p>
</li><li>
<p>DateTime: <code>require &#39;json/add/date_time&#39;</code></p>
</li><li>
<p>Exception: <code>require &#39;json/add/exception&#39;</code></p>
</li><li>
<p>OpenStruct: <code>require &#39;json/add/ostruct&#39;</code></p>
</li><li>
<p>Range: <code>require &#39;json/add/range&#39;</code></p>
</li><li>
<p>Rational: <code>require &#39;json/add/rational&#39;</code></p>
</li><li>
<p>Regexp: <code>require &#39;json/add/regexp&#39;</code></p>
</li><li>
<p>Set: <code>require &#39;json/add/set&#39;</code></p>
</li><li>
<p>Struct: <code>require &#39;json/add/struct&#39;</code></p>
</li><li>
<p>Symbol: <code>require &#39;json/add/symbol&#39;</code></p>
</li><li>
<p>Time: <code>require &#39;json/add/time&#39;</code></p>
</li></ul>

<p>To reduce punctuation clutter, the examples below show the generated JSON via <code>puts</code>, rather than the usual <code>inspect</code>,</p>

<p>BigDecimal:</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;json/add/bigdecimal&#39;</span>
<span class="ruby-identifier">ruby0</span> = <span class="ruby-constant">BigDecimal</span>(<span class="ruby-value">0</span>) <span class="ruby-comment"># 0.0</span>
<span class="ruby-identifier">json</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">ruby0</span>) <span class="ruby-comment"># {&quot;json_class&quot;:&quot;BigDecimal&quot;,&quot;b&quot;:&quot;27:0.0&quot;}</span>
<span class="ruby-identifier">ruby1</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">json</span>, <span class="ruby-value">create_additions:</span> <span class="ruby-keyword">true</span>) <span class="ruby-comment"># 0.0</span>
<span class="ruby-identifier">ruby1</span>.<span class="ruby-identifier">class</span> <span class="ruby-comment"># =&gt; BigDecimal</span>
</pre>

<p>Complex:</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;json/add/complex&#39;</span>
<span class="ruby-identifier">ruby0</span> = <span class="ruby-constant">Complex</span>(<span class="ruby-value">1</span><span class="ruby-value">+0i</span>) <span class="ruby-comment"># 1+0i</span>
<span class="ruby-identifier">json</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">ruby0</span>) <span class="ruby-comment"># {&quot;json_class&quot;:&quot;Complex&quot;,&quot;r&quot;:1,&quot;i&quot;:0}</span>
<span class="ruby-identifier">ruby1</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">json</span>, <span class="ruby-value">create_additions:</span> <span class="ruby-keyword">true</span>) <span class="ruby-comment"># 1+0i</span>
<span class="ruby-identifier">ruby1</span>.<span class="ruby-identifier">class</span> <span class="ruby-comment"># Complex</span>
</pre>

<p>Date:</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;json/add/date&#39;</span>
<span class="ruby-identifier">ruby0</span> = <span class="ruby-constant">Date</span>.<span class="ruby-identifier">today</span> <span class="ruby-comment"># 2020-05-02</span>
<span class="ruby-identifier">json</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">ruby0</span>) <span class="ruby-comment"># {&quot;json_class&quot;:&quot;Date&quot;,&quot;y&quot;:2020,&quot;m&quot;:5,&quot;d&quot;:2,&quot;sg&quot;:2299161.0}</span>
<span class="ruby-identifier">ruby1</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">json</span>, <span class="ruby-value">create_additions:</span> <span class="ruby-keyword">true</span>) <span class="ruby-comment"># 2020-05-02</span>
<span class="ruby-identifier">ruby1</span>.<span class="ruby-identifier">class</span> <span class="ruby-comment"># Date</span>
</pre>

<p>DateTime:</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;json/add/date_time&#39;</span>
<span class="ruby-identifier">ruby0</span> = <span class="ruby-constant">DateTime</span>.<span class="ruby-identifier">now</span> <span class="ruby-comment"># 2020-05-02T10:38:13-05:00</span>
<span class="ruby-identifier">json</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">ruby0</span>) <span class="ruby-comment"># {&quot;json_class&quot;:&quot;DateTime&quot;,&quot;y&quot;:2020,&quot;m&quot;:5,&quot;d&quot;:2,&quot;H&quot;:10,&quot;M&quot;:38,&quot;S&quot;:13,&quot;of&quot;:&quot;-5/24&quot;,&quot;sg&quot;:2299161.0}</span>
<span class="ruby-identifier">ruby1</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">json</span>, <span class="ruby-value">create_additions:</span> <span class="ruby-keyword">true</span>) <span class="ruby-comment"># 2020-05-02T10:38:13-05:00</span>
<span class="ruby-identifier">ruby1</span>.<span class="ruby-identifier">class</span> <span class="ruby-comment"># DateTime</span>
</pre>

<p>Exception (and its subclasses including RuntimeError):</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;json/add/exception&#39;</span>
<span class="ruby-identifier">ruby0</span> = <span class="ruby-constant">Exception</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&#39;A message&#39;</span>) <span class="ruby-comment"># A message</span>
<span class="ruby-identifier">json</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">ruby0</span>) <span class="ruby-comment"># {&quot;json_class&quot;:&quot;Exception&quot;,&quot;m&quot;:&quot;A message&quot;,&quot;b&quot;:null}</span>
<span class="ruby-identifier">ruby1</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">json</span>, <span class="ruby-value">create_additions:</span> <span class="ruby-keyword">true</span>) <span class="ruby-comment"># A message</span>
<span class="ruby-identifier">ruby1</span>.<span class="ruby-identifier">class</span> <span class="ruby-comment"># Exception</span>
<span class="ruby-identifier">ruby0</span> = <span class="ruby-constant">RuntimeError</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&#39;Another message&#39;</span>) <span class="ruby-comment"># Another message</span>
<span class="ruby-identifier">json</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">ruby0</span>) <span class="ruby-comment"># {&quot;json_class&quot;:&quot;RuntimeError&quot;,&quot;m&quot;:&quot;Another message&quot;,&quot;b&quot;:null}</span>
<span class="ruby-identifier">ruby1</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">json</span>, <span class="ruby-value">create_additions:</span> <span class="ruby-keyword">true</span>) <span class="ruby-comment"># Another message</span>
<span class="ruby-identifier">ruby1</span>.<span class="ruby-identifier">class</span> <span class="ruby-comment"># RuntimeError</span>
</pre>

<p>OpenStruct:</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;json/add/ostruct&#39;</span>
<span class="ruby-identifier">ruby0</span> = <span class="ruby-constant">OpenStruct</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value">name:</span> <span class="ruby-string">&#39;Matz&#39;</span>, <span class="ruby-value">language:</span> <span class="ruby-string">&#39;Ruby&#39;</span>) <span class="ruby-comment"># #&lt;OpenStruct name=&quot;Matz&quot;, language=&quot;Ruby&quot;&gt;</span>
<span class="ruby-identifier">json</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">ruby0</span>) <span class="ruby-comment"># {&quot;json_class&quot;:&quot;OpenStruct&quot;,&quot;t&quot;:{&quot;name&quot;:&quot;Matz&quot;,&quot;language&quot;:&quot;Ruby&quot;}}</span>
<span class="ruby-identifier">ruby1</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">json</span>, <span class="ruby-value">create_additions:</span> <span class="ruby-keyword">true</span>) <span class="ruby-comment"># #&lt;OpenStruct name=&quot;Matz&quot;, language=&quot;Ruby&quot;&gt;</span>
<span class="ruby-identifier">ruby1</span>.<span class="ruby-identifier">class</span> <span class="ruby-comment"># OpenStruct</span>
</pre>

<p>Range:</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;json/add/range&#39;</span>
<span class="ruby-identifier">ruby0</span> = <span class="ruby-constant">Range</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value">0</span>, <span class="ruby-value">2</span>) <span class="ruby-comment"># 0..2</span>
<span class="ruby-identifier">json</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">ruby0</span>) <span class="ruby-comment"># {&quot;json_class&quot;:&quot;Range&quot;,&quot;a&quot;:[0,2,false]}</span>
<span class="ruby-identifier">ruby1</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">json</span>, <span class="ruby-value">create_additions:</span> <span class="ruby-keyword">true</span>) <span class="ruby-comment"># 0..2</span>
<span class="ruby-identifier">ruby1</span>.<span class="ruby-identifier">class</span> <span class="ruby-comment"># Range</span>
</pre>

<p>Rational:</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;json/add/rational&#39;</span>
<span class="ruby-identifier">ruby0</span> = <span class="ruby-constant">Rational</span>(<span class="ruby-value">1</span>, <span class="ruby-value">3</span>) <span class="ruby-comment"># 1/3</span>
<span class="ruby-identifier">json</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">ruby0</span>) <span class="ruby-comment"># {&quot;json_class&quot;:&quot;Rational&quot;,&quot;n&quot;:1,&quot;d&quot;:3}</span>
<span class="ruby-identifier">ruby1</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">json</span>, <span class="ruby-value">create_additions:</span> <span class="ruby-keyword">true</span>) <span class="ruby-comment"># 1/3</span>
<span class="ruby-identifier">ruby1</span>.<span class="ruby-identifier">class</span> <span class="ruby-comment"># Rational</span>
</pre>

<p>Regexp:</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;json/add/regexp&#39;</span>
<span class="ruby-identifier">ruby0</span> = <span class="ruby-constant">Regexp</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&#39;foo&#39;</span>) <span class="ruby-comment"># (?-mix:foo)</span>
<span class="ruby-identifier">json</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">ruby0</span>) <span class="ruby-comment"># {&quot;json_class&quot;:&quot;Regexp&quot;,&quot;o&quot;:0,&quot;s&quot;:&quot;foo&quot;}</span>
<span class="ruby-identifier">ruby1</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">json</span>, <span class="ruby-value">create_additions:</span> <span class="ruby-keyword">true</span>) <span class="ruby-comment"># (?-mix:foo)</span>
<span class="ruby-identifier">ruby1</span>.<span class="ruby-identifier">class</span> <span class="ruby-comment"># Regexp</span>
</pre>

<p>Set:</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;json/add/set&#39;</span>
<span class="ruby-identifier">ruby0</span> = <span class="ruby-constant">Set</span>.<span class="ruby-identifier">new</span>([<span class="ruby-value">0</span>, <span class="ruby-value">1</span>, <span class="ruby-value">2</span>]) <span class="ruby-comment"># #&lt;Set: {0, 1, 2}&gt;</span>
<span class="ruby-identifier">json</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">ruby0</span>) <span class="ruby-comment"># {&quot;json_class&quot;:&quot;Set&quot;,&quot;a&quot;:[0,1,2]}</span>
<span class="ruby-identifier">ruby1</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">json</span>, <span class="ruby-value">create_additions:</span> <span class="ruby-keyword">true</span>) <span class="ruby-comment"># #&lt;Set: {0, 1, 2}&gt;</span>
<span class="ruby-identifier">ruby1</span>.<span class="ruby-identifier">class</span> <span class="ruby-comment"># Set</span>
</pre>

<p>Struct:</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;json/add/struct&#39;</span>
<span class="ruby-constant">Customer</span> = <span class="ruby-constant">Struct</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value">:name</span>, <span class="ruby-value">:address</span>) <span class="ruby-comment"># Customer</span>
<span class="ruby-identifier">ruby0</span> = <span class="ruby-constant">Customer</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&quot;Dave&quot;</span>, <span class="ruby-string">&quot;123 Main&quot;</span>) <span class="ruby-comment"># #&lt;struct Customer name=&quot;Dave&quot;, address=&quot;123 Main&quot;&gt;</span>
<span class="ruby-identifier">json</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">ruby0</span>) <span class="ruby-comment"># {&quot;json_class&quot;:&quot;Customer&quot;,&quot;v&quot;:[&quot;Dave&quot;,&quot;123 Main&quot;]}</span>
<span class="ruby-identifier">ruby1</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">json</span>, <span class="ruby-value">create_additions:</span> <span class="ruby-keyword">true</span>) <span class="ruby-comment"># #&lt;struct Customer name=&quot;Dave&quot;, address=&quot;123 Main&quot;&gt;</span>
<span class="ruby-identifier">ruby1</span>.<span class="ruby-identifier">class</span> <span class="ruby-comment"># Customer</span>
</pre>

<p>Symbol:</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;json/add/symbol&#39;</span>
<span class="ruby-identifier">ruby0</span> = <span class="ruby-value">:foo</span> <span class="ruby-comment"># foo</span>
<span class="ruby-identifier">json</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">ruby0</span>) <span class="ruby-comment"># {&quot;json_class&quot;:&quot;Symbol&quot;,&quot;s&quot;:&quot;foo&quot;}</span>
<span class="ruby-identifier">ruby1</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">json</span>, <span class="ruby-value">create_additions:</span> <span class="ruby-keyword">true</span>) <span class="ruby-comment"># foo</span>
<span class="ruby-identifier">ruby1</span>.<span class="ruby-identifier">class</span> <span class="ruby-comment"># Symbol</span>
</pre>

<p>Time:</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;json/add/time&#39;</span>
<span class="ruby-identifier">ruby0</span> = <span class="ruby-constant">Time</span>.<span class="ruby-identifier">now</span> <span class="ruby-comment"># 2020-05-02 11:28:26 -0500</span>
<span class="ruby-identifier">json</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">ruby0</span>) <span class="ruby-comment"># {&quot;json_class&quot;:&quot;Time&quot;,&quot;s&quot;:1588436906,&quot;n&quot;:840560000}</span>
<span class="ruby-identifier">ruby1</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">json</span>, <span class="ruby-value">create_additions:</span> <span class="ruby-keyword">true</span>) <span class="ruby-comment"># 2020-05-02 11:28:26 -0500</span>
<span class="ruby-identifier">ruby1</span>.<span class="ruby-identifier">class</span> <span class="ruby-comment"># Time</span>
</pre>

<h3 id="module-JSON-label-Custom+JSON+Additions">Custom JSON Additions<span><a href="#module-JSON-label-Custom+JSON+Additions">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>In addition to the JSON additions provided, you can craft JSON additions of your own, either for Ruby built-in classes or for user-defined classes.</p>

<p>Here’s a user-defined class <code>Foo</code>:</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Foo</span>
  <span class="ruby-identifier">attr_accessor</span> <span class="ruby-value">:bar</span>, <span class="ruby-value">:baz</span>
  <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">initialize</span>(<span class="ruby-identifier">bar</span>, <span class="ruby-identifier">baz</span>)
    <span class="ruby-keyword">self</span>.<span class="ruby-identifier">bar</span> = <span class="ruby-identifier">bar</span>
    <span class="ruby-keyword">self</span>.<span class="ruby-identifier">baz</span> = <span class="ruby-identifier">baz</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
</pre>

<p>Here’s the JSON addition for it:</p>

<pre class="ruby"><span class="ruby-comment"># Extend class Foo with JSON addition.</span>
<span class="ruby-keyword">class</span> <span class="ruby-constant">Foo</span>
  <span class="ruby-comment"># Serialize Foo object with its class name and arguments</span>
  <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">to_json</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">args</span>)
    {
      <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">create_id</span>  <span class="ruby-operator">=&gt;</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">name</span>,
      <span class="ruby-string">&#39;a&#39;</span>             <span class="ruby-operator">=&gt;</span> [ <span class="ruby-identifier">bar</span>, <span class="ruby-identifier">baz</span> ]
    }.<span class="ruby-identifier">to_json</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">args</span>)
  <span class="ruby-keyword">end</span>
  <span class="ruby-comment"># Deserialize JSON string by constructing new Foo object with arguments.</span>
  <span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">json_create</span>(<span class="ruby-identifier">object</span>)
    <span class="ruby-identifier">new</span>(<span class="ruby-operator">*</span><span class="ruby-identifier">object</span>[<span class="ruby-string">&#39;a&#39;</span>])
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
</pre>

<p>Demonstration:</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;json&#39;</span>
<span class="ruby-comment"># This Foo object has no custom addition.</span>
<span class="ruby-identifier">foo0</span> = <span class="ruby-constant">Foo</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value">0</span>, <span class="ruby-value">1</span>)
<span class="ruby-identifier">json0</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">foo0</span>)
<span class="ruby-identifier">obj0</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">json0</span>)
<span class="ruby-comment"># Lood the custom addition.</span>
<span class="ruby-identifier">require_relative</span> <span class="ruby-string">&#39;foo_addition&#39;</span>
<span class="ruby-comment"># This foo has the custom addition.</span>
<span class="ruby-identifier">foo1</span> = <span class="ruby-constant">Foo</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value">0</span>, <span class="ruby-value">1</span>)
<span class="ruby-identifier">json1</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">foo1</span>)
<span class="ruby-identifier">obj1</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">json1</span>, <span class="ruby-value">create_additions:</span> <span class="ruby-keyword">true</span>)
<span class="ruby-comment">#   Make a nice display.</span>
<span class="ruby-identifier">display</span> = <span class="ruby-identifier">&lt;&lt;EOT</span>
<span class="ruby-value">Generated JSON:
  Without custom addition:  #{json0} (#{json0.class})
  With custom addition:     #{json1} (#{json1.class})
Parsed JSON:
  Without custom addition:  #{obj0.inspect} (#{obj0.class})
  With custom addition:     #{obj1.inspect} (#{obj1.class})
</span><span class="ruby-identifier">EOT</span>
<span class="ruby-identifier">puts</span> <span class="ruby-identifier">display</span>
</pre>

<p>Output:</p>

<pre>Generated JSON:
  Without custom addition:  &quot;#&lt;Foo:0x0000000006534e80&gt;&quot; (String)
  With custom addition:     {&quot;json_class&quot;:&quot;Foo&quot;,&quot;a&quot;:[0,1]} (String)
Parsed JSON:
  Without custom addition:  &quot;#&lt;Foo:0x0000000006534e80&gt;&quot; (String)
  With custom addition:     #&lt;Foo:0x0000000006473bb8 @bar=0, @baz=1&gt; (Foo)</pre>

    </section>

      <section id="5Buntitled-5D" class="documentation-section">


      <section class="constants-list">
      <header>
      <h3>Constants</h3>
      </header>
      <dl>
          <dt id="CREATE_ID_TLS_KEY">CREATE_ID_TLS_KEY
          <dd>
          <dt id="DEFAULT_CREATE_ID">DEFAULT_CREATE_ID
          <dd>
          <dt id="Infinity">Infinity
          <dd>
          <dt id="JSON_LOADED">JSON_LOADED
          <dd>
          <dt id="MinusInfinity">MinusInfinity
          <dd>
          <dt id="NaN">NaN
          <dd>
          <dt id="VERSION">VERSION
          <dd><p><a href="JSON.html"><code>JSON</code></a> version</p>
        </dl>
        </section>

        <section class="attribute-method-details" class="method-section">
        <header>
        <h3>Attributes</h3>
        </header>

          <div id="attribute-c-dump_default_options" class="method-detail">
            <div class="method-heading attribute-method-heading">
              <span class="method-name">dump_default_options</span><span
                class="attribute-access-type">[RW]</span>
            </div>

            <div class="method-description">
              <p>Sets or returns the default options for the <a href="JSON.html#method-i-dump"><code>JSON.dump</code></a> method. Initially:</p>

<pre class="ruby"><span class="ruby-identifier">opts</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">dump_default_options</span>
<span class="ruby-identifier">opts</span> <span class="ruby-comment"># =&gt; {:max_nesting=&gt;false, :allow_nan=&gt;true, :escape_slash=&gt;false}</span>
</pre>
              </div>
            </div>
          <div id="attribute-c-generator" class="method-detail">
            <div class="method-heading attribute-method-heading">
              <span class="method-name">generator</span><span
                class="attribute-access-type">[R]</span>
            </div>

            <div class="method-description">
              <p>Returns the <a href="JSON.html"><code>JSON</code></a> generator module that is used by <a href="JSON.html"><code>JSON</code></a>. This is either <a href="JSON/Ext/Generator.html"><code>JSON::Ext::Generator</code></a> or JSON::Pure::Generator:</p>

<pre class="ruby"><span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generator</span> <span class="ruby-comment"># =&gt; JSON::Ext::Generator</span>
</pre>
              </div>
            </div>
          <div id="attribute-c-load_default_options" class="method-detail">
            <div class="method-heading attribute-method-heading">
              <span class="method-name">load_default_options</span><span
                class="attribute-access-type">[RW]</span>
            </div>

            <div class="method-description">
              <p>Sets or returns default options for the <a href="JSON.html#method-i-load"><code>JSON.load</code></a> method. Initially:</p>

<pre class="ruby"><span class="ruby-identifier">opts</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">load_default_options</span>
<span class="ruby-identifier">opts</span> <span class="ruby-comment"># =&gt; {:max_nesting=&gt;false, :allow_nan=&gt;true, :allow_blank=&gt;true, :create_additions=&gt;true}</span>
</pre>
              </div>
            </div>
          <div id="attribute-c-parser" class="method-detail">
            <div class="method-heading attribute-method-heading">
              <span class="method-name">parser</span><span
                class="attribute-access-type">[R]</span>
            </div>

            <div class="method-description">
              <p>Returns the <a href="JSON.html"><code>JSON</code></a> parser class that is used by <a href="JSON.html"><code>JSON</code></a>. This is either <a href="JSON/Ext/Parser.html"><code>JSON::Ext::Parser</code></a> or JSON::Pure::Parser:</p>

<pre class="ruby"><span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parser</span> <span class="ruby-comment"># =&gt; JSON::Ext::Parser</span>
</pre>
              </div>
            </div>
          <div id="attribute-c-state" class="method-detail">
            <div class="method-heading attribute-method-heading">
              <span class="method-name">state</span><span
                class="attribute-access-type">[RW]</span>
            </div>

            <div class="method-description">
              <p>Sets or Returns the <a href="JSON.html"><code>JSON</code></a> generator state class that is used by <a href="JSON.html"><code>JSON</code></a>. This is either <a href="JSON/Ext/Generator/State.html"><code>JSON::Ext::Generator::State</code></a> or JSON::Pure::Generator::State:</p>

<pre class="ruby"><span class="ruby-constant">JSON</span>.<span class="ruby-identifier">state</span> <span class="ruby-comment"># =&gt; JSON::Ext::Generator::State</span>
</pre>
              </div>
            </div>
            </section>


                <section id="public-class-5Buntitled-5D-method-details" class="method-section">
                <header>
                <h3>Public Class Methods</h3>
                </header>

                  <div id="method-c-5B-5D" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          JSON[object] &rarr; new_array or new_string
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>If <code>object</code> is a String, calls <a href="JSON.html#method-i-parse"><code>JSON.parse</code></a> with <code>object</code> and <code>opts</code> (see method <a href="JSON.html#method-i-parse"><code>parse</code></a>):</p>

<pre class="ruby"><span class="ruby-identifier">json</span> = <span class="ruby-string">&#39;[0, 1, null]&#39;</span>
<span class="ruby-constant">JSON</span>[<span class="ruby-identifier">json</span>]<span class="ruby-comment"># =&gt; [0, 1, nil]</span>
</pre>

<p>Otherwise, calls <a href="JSON.html#method-i-generate"><code>JSON.generate</code></a> with <code>object</code> and <code>opts</code> (see method <a href="JSON.html#method-i-generate"><code>generate</code></a>):</p>

<pre class="ruby"><span class="ruby-identifier">ruby</span> = [<span class="ruby-value">0</span>, <span class="ruby-value">1</span>, <span class="ruby-keyword">nil</span>]
<span class="ruby-constant">JSON</span>[<span class="ruby-identifier">ruby</span>] <span class="ruby-comment"># =&gt; &#39;[0,1,null]&#39;</span>
</pre>

                              <div class="method-source-code" id="5B-5D-source">
            <pre><span class="ruby-comment"># File json/lib/json/common.rb, line 18</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">[]</span>(<span class="ruby-identifier">object</span>, <span class="ruby-identifier">opts</span> = {})
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">object</span>.<span class="ruby-identifier">respond_to?</span> <span class="ruby-value">:to_str</span>
    <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">object</span>.<span class="ruby-identifier">to_str</span>, <span class="ruby-identifier">opts</span>)
  <span class="ruby-keyword">else</span>
    <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">object</span>, <span class="ruby-identifier">opts</span>)
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-create_fast_state" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">create_fast_state</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              

                              <div class="method-source-code" id="create_fast_state-source">
            <pre><span class="ruby-comment"># File json/lib/json/common.rb, line 81</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">create_fast_state</span>
  <span class="ruby-constant">State</span>.<span class="ruby-identifier">new</span>(
    <span class="ruby-value">:indent</span>         <span class="ruby-operator">=&gt;</span> <span class="ruby-string">&#39;&#39;</span>,
    <span class="ruby-value">:space</span>          <span class="ruby-operator">=&gt;</span> <span class="ruby-string">&#39;&#39;</span>,
    <span class="ruby-value">:object_nl</span>      <span class="ruby-operator">=&gt;</span> <span class="ruby-string">&quot;&quot;</span>,
    <span class="ruby-value">:array_nl</span>       <span class="ruby-operator">=&gt;</span> <span class="ruby-string">&quot;&quot;</span>,
    <span class="ruby-value">:max_nesting</span>    <span class="ruby-operator">=&gt;</span> <span class="ruby-keyword">false</span>
  )
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-create_id" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">create_id</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns the current create identifier. See also <a href="JSON.html#method-c-create_id-3D"><code>JSON.create_id=</code></a>.</p>

                              <div class="method-source-code" id="create_id-source">
            <pre><span class="ruby-comment"># File json/lib/json/common.rb, line 126</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">create_id</span>
  <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">current</span>[<span class="ruby-constant">CREATE_ID_TLS_KEY</span>] <span class="ruby-operator">||</span> <span class="ruby-constant">DEFAULT_CREATE_ID</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-create_id-3D" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">create_id=</span><span
                                class="method-args">(new_value)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Sets create identifier, which is used to decide if the <em>json_create</em> hook of a class should be called; initial value is <code>json_class</code>:</p>

<pre class="ruby"><span class="ruby-constant">JSON</span>.<span class="ruby-identifier">create_id</span> <span class="ruby-comment"># =&gt; &#39;json_class&#39;</span>
</pre>

                              <div class="method-source-code" id="create_id-3D-source">
            <pre><span class="ruby-comment"># File json/lib/json/common.rb, line 120</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">create_id=</span>(<span class="ruby-identifier">new_value</span>)
  <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">current</span>[<span class="ruby-constant">CREATE_ID_TLS_KEY</span>] = <span class="ruby-identifier">new_value</span>.<span class="ruby-identifier">dup</span>.<span class="ruby-identifier">freeze</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-create_pretty_state" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">create_pretty_state</span><span
                                class="method-args">()</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              

                              <div class="method-source-code" id="create_pretty_state-source">
            <pre><span class="ruby-comment"># File json/lib/json/common.rb, line 91</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">create_pretty_state</span>
  <span class="ruby-constant">State</span>.<span class="ruby-identifier">new</span>(
    <span class="ruby-value">:indent</span>         <span class="ruby-operator">=&gt;</span> <span class="ruby-string">&#39;  &#39;</span>,
    <span class="ruby-value">:space</span>          <span class="ruby-operator">=&gt;</span> <span class="ruby-string">&#39; &#39;</span>,
    <span class="ruby-value">:object_nl</span>      <span class="ruby-operator">=&gt;</span> <span class="ruby-string">&quot;\n&quot;</span>,
    <span class="ruby-value">:array_nl</span>       <span class="ruby-operator">=&gt;</span> <span class="ruby-string">&quot;\n&quot;</span>
  )
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-iconv" class="method-detail ">
                            <div class="method-heading">
                              <span class="method-name">iconv</span><span
                                class="method-args">(to, from, string)</span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Encodes string using String.encode.</p>

                              <div class="method-source-code" id="iconv-source">
            <pre><span class="ruby-comment"># File json/lib/json/common.rb, line 633</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">iconv</span>(<span class="ruby-identifier">to</span>, <span class="ruby-identifier">from</span>, <span class="ruby-identifier">string</span>)
  <span class="ruby-identifier">string</span>.<span class="ruby-identifier">encode</span>(<span class="ruby-identifier">to</span>, <span class="ruby-identifier">from</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-c-restore" class="method-detail method-alias">
                            <div class="method-heading">
                              <span class="method-name">restore</span><span
                                class="method-args">(source, proc = nil, options = {})</span>
                            </div>

                            <div class="method-description">
                              

                            </div>


                            <div class="aliases">
                              Alias for: <a href="JSON.html#method-i-load">load</a>
                            </div>
                          </div>

                          </section>

                <section id="public-instance-5Buntitled-5D-method-details" class="method-section">
                <header>
                <h3>Public Instance Methods</h3>
                </header>

                  <div id="method-i-dump" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          dump(obj, io = nil, limit = nil)
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Dumps <code>obj</code> as a JSON string, i.e. calls generate on the object and returns the result.</p>

<p>The default options can be changed via method <a href="JSON.html#attribute-c-dump_default_options"><code>JSON.dump_default_options</code></a>.</p>
<ul><li>
<p>Argument <code>io</code>, if given, should respond to method <code>write</code>; the JSON String is written to <code>io</code>, and <code>io</code> is returned. If <code>io</code> is not given, the JSON String is returned.</p>
</li><li>
<p>Argument <code>limit</code>, if given, is passed to <a href="JSON.html#method-i-generate"><code>JSON.generate</code></a> as option <code>max_nesting</code>.</p>
</li></ul>
<hr>

<p>When argument <code>io</code> is not given, returns the JSON String generated from <code>obj</code>:</p>

<pre class="ruby"><span class="ruby-identifier">obj</span> = {<span class="ruby-value">foo:</span> [<span class="ruby-value">0</span>, <span class="ruby-value">1</span>], <span class="ruby-value">bar:</span> {<span class="ruby-value">baz:</span> <span class="ruby-value">2</span>, <span class="ruby-value">bat:</span> <span class="ruby-value">3</span>}, <span class="ruby-value">bam:</span> <span class="ruby-value">:bad</span>}
<span class="ruby-identifier">json</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">dump</span>(<span class="ruby-identifier">obj</span>)
<span class="ruby-identifier">json</span> <span class="ruby-comment"># =&gt; &quot;{\&quot;foo\&quot;:[0,1],\&quot;bar\&quot;:{\&quot;baz\&quot;:2,\&quot;bat\&quot;:3},\&quot;bam\&quot;:\&quot;bad\&quot;}&quot;</span>
</pre>

<p>When argument <code>io</code> is given, writes the JSON String to <code>io</code> and returns <code>io</code>:</p>

<pre class="ruby"><span class="ruby-identifier">path</span> = <span class="ruby-string">&#39;t.json&#39;</span>
<span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">path</span>, <span class="ruby-string">&#39;w&#39;</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">file</span><span class="ruby-operator">|</span>
  <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">dump</span>(<span class="ruby-identifier">obj</span>, <span class="ruby-identifier">file</span>)
<span class="ruby-keyword">end</span> <span class="ruby-comment"># =&gt; #&lt;File:t.json (closed)&gt;</span>
<span class="ruby-identifier">puts</span> <span class="ruby-constant">File</span>.<span class="ruby-identifier">read</span>(<span class="ruby-identifier">path</span>)
</pre>

<p>Output:</p>

<pre class="ruby">{<span class="ruby-value">&quot;foo&quot;:</span>[<span class="ruby-value">0</span>,<span class="ruby-value">1</span>],<span class="ruby-value">&quot;bar&quot;:</span>{<span class="ruby-value">&quot;baz&quot;:</span><span class="ruby-value">2</span>,<span class="ruby-value">&quot;bat&quot;:</span><span class="ruby-value">3</span>},<span class="ruby-value">&quot;bam&quot;:</span><span class="ruby-string">&quot;bad&quot;</span>}
</pre>

                              <div class="method-source-code" id="dump-source">
            <pre><span class="ruby-comment"># File json/lib/json/common.rb, line 611</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">dump</span>(<span class="ruby-identifier">obj</span>, <span class="ruby-identifier">anIO</span> = <span class="ruby-keyword">nil</span>, <span class="ruby-identifier">limit</span> = <span class="ruby-keyword">nil</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">anIO</span> <span class="ruby-keyword">and</span> <span class="ruby-identifier">limit</span>.<span class="ruby-identifier">nil?</span>
    <span class="ruby-identifier">anIO</span> = <span class="ruby-identifier">anIO</span>.<span class="ruby-identifier">to_io</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">anIO</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-value">:to_io</span>)
    <span class="ruby-keyword">unless</span> <span class="ruby-identifier">anIO</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-value">:write</span>)
      <span class="ruby-identifier">limit</span> = <span class="ruby-identifier">anIO</span>
      <span class="ruby-identifier">anIO</span> = <span class="ruby-keyword">nil</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-identifier">opts</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">dump_default_options</span>
  <span class="ruby-identifier">opts</span> = <span class="ruby-identifier">opts</span>.<span class="ruby-identifier">merge</span>(<span class="ruby-value">:max_nesting</span> <span class="ruby-operator">=&gt;</span> <span class="ruby-identifier">limit</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">limit</span>
  <span class="ruby-identifier">result</span> = <span class="ruby-identifier">generate</span>(<span class="ruby-identifier">obj</span>, <span class="ruby-identifier">opts</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">anIO</span>
    <span class="ruby-identifier">anIO</span>.<span class="ruby-identifier">write</span> <span class="ruby-identifier">result</span>
    <span class="ruby-identifier">anIO</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">result</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">rescue</span> <span class="ruby-constant">JSON</span><span class="ruby-operator">::</span><span class="ruby-constant">NestingError</span>
  <span class="ruby-identifier">raise</span> <span class="ruby-constant">ArgumentError</span>, <span class="ruby-string">&quot;exceed depth limit&quot;</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-fast_generate" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          fast_generate(obj, opts) &rarr; new_string
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Arguments <code>obj</code> and <code>opts</code> here are the same as arguments <code>obj</code> and <code>opts</code> in <a href="JSON.html#method-i-generate"><code>JSON.generate</code></a>.</p>

<p>By default, generates JSON data without checking for circular references in <code>obj</code> (option <code>max_nesting</code> set to <code>false</code>, disabled).</p>

<p>Raises an exception if <code>obj</code> contains circular references:</p>

<pre class="ruby"><span class="ruby-identifier">a</span> = []; <span class="ruby-identifier">b</span> = []; <span class="ruby-identifier">a</span>.<span class="ruby-identifier">push</span>(<span class="ruby-identifier">b</span>); <span class="ruby-identifier">b</span>.<span class="ruby-identifier">push</span>(<span class="ruby-identifier">a</span>)
<span class="ruby-comment"># Raises SystemStackError (stack level too deep):</span>
<span class="ruby-constant">JSON</span>.<span class="ruby-identifier">fast_generate</span>(<span class="ruby-identifier">a</span>)
</pre>

                              <div class="method-source-code" id="fast_generate-source">
            <pre><span class="ruby-comment"># File json/lib/json/common.rb, line 325</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">fast_generate</span>(<span class="ruby-identifier">obj</span>, <span class="ruby-identifier">opts</span> = <span class="ruby-keyword">nil</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-constant">State</span> <span class="ruby-operator">===</span> <span class="ruby-identifier">opts</span>
    <span class="ruby-identifier">state</span> = <span class="ruby-identifier">opts</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">state</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">create_fast_state</span>.<span class="ruby-identifier">configure</span>(<span class="ruby-identifier">opts</span>)
  <span class="ruby-keyword">end</span>
  <span class="ruby-identifier">state</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">obj</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-generate" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          generate(obj, opts = nil) &rarr; new_string
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns a String containing the generated JSON data.</p>

<p>See also <a href="JSON.html#method-i-fast_generate"><code>JSON.fast_generate</code></a>, <a href="JSON.html#method-i-pretty_generate"><code>JSON.pretty_generate</code></a>.</p>

<p>Argument <code>obj</code> is the Ruby object to be converted to JSON.</p>

<p>Argument <code>opts</code>, if given, contains a Hash of options for the generation. See <a href="#module-JSON-label-Generating+Options">Generating Options</a>.</p>
<hr>

<p>When <code>obj</code> is an Array, returns a String containing a JSON array:</p>

<pre class="ruby"><span class="ruby-identifier">obj</span> = [<span class="ruby-string">&quot;foo&quot;</span>, <span class="ruby-value">1.0</span>, <span class="ruby-keyword">true</span>, <span class="ruby-keyword">false</span>, <span class="ruby-keyword">nil</span>]
<span class="ruby-identifier">json</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">obj</span>)
<span class="ruby-identifier">json</span> <span class="ruby-comment"># =&gt; &#39;[&quot;foo&quot;,1.0,true,false,null]&#39;</span>
</pre>

<p>When <code>obj</code> is a Hash, returns a String containing a JSON object:</p>

<pre class="ruby"><span class="ruby-identifier">obj</span> = {<span class="ruby-value">foo:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bar:</span> <span class="ruby-string">&#39;s&#39;</span>, <span class="ruby-value">baz:</span> <span class="ruby-value">:bat</span>}
<span class="ruby-identifier">json</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">obj</span>)
<span class="ruby-identifier">json</span> <span class="ruby-comment"># =&gt; &#39;{&quot;foo&quot;:0,&quot;bar&quot;:&quot;s&quot;,&quot;baz&quot;:&quot;bat&quot;}&#39;</span>
</pre>

<p>For examples of generating from other Ruby objects, see <a href="#module-JSON-label-Generating+JSON+from+Other+Objects">Generating JSON from Other Objects</a>.</p>
<hr>

<p>Raises an exception if any formatting option is not a String.</p>

<p>Raises an exception if <code>obj</code> contains circular references:</p>

<pre class="ruby"><span class="ruby-identifier">a</span> = []; <span class="ruby-identifier">b</span> = []; <span class="ruby-identifier">a</span>.<span class="ruby-identifier">push</span>(<span class="ruby-identifier">b</span>); <span class="ruby-identifier">b</span>.<span class="ruby-identifier">push</span>(<span class="ruby-identifier">a</span>)
<span class="ruby-comment"># Raises JSON::NestingError (nesting of 100 is too deep):</span>
<span class="ruby-constant">JSON</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">a</span>)
</pre>

                              <div class="method-source-code" id="generate-source">
            <pre><span class="ruby-comment"># File json/lib/json/common.rb, line 296</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">generate</span>(<span class="ruby-identifier">obj</span>, <span class="ruby-identifier">opts</span> = <span class="ruby-keyword">nil</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-constant">State</span> <span class="ruby-operator">===</span> <span class="ruby-identifier">opts</span>
    <span class="ruby-identifier">state</span> = <span class="ruby-identifier">opts</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">state</span> = <span class="ruby-constant">State</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">opts</span>)
  <span class="ruby-keyword">end</span>
  <span class="ruby-identifier">state</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">obj</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-load" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          load(source, proc = nil, options = {}) &rarr; object
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns the Ruby objects created by parsing the given <code>source</code>.</p>
<ul><li>
<p>Argument <code>source</code> must be, or be convertible to, a String:</p>
<ul><li>
<p>If <code>source</code> responds to instance method <code>to_str</code>, <code>source.to_str</code> becomes the source.</p>
</li><li>
<p>If <code>source</code> responds to instance method <code>to_io</code>, <code>source.to_io.read</code> becomes the source.</p>
</li><li>
<p>If <code>source</code> responds to instance method <code>read</code>, <code>source.read</code> becomes the source.</p>
</li><li>
<p>If both of the following are true, source becomes the String <code>&#39;null&#39;</code>:</p>
<ul><li>
<p>Option <code>allow_blank</code> specifies a truthy value.</p>
</li><li>
<p>The source, as defined above, is <code>nil</code> or the empty String <code>&#39;&#39;</code>.</p>
</li></ul>
</li><li>
<p>Otherwise, <code>source</code> remains the source.</p>
</li></ul>
</li><li>
<p>Argument <code>proc</code>, if given, must be a Proc that accepts one argument. It will be called recursively with each result (depth-first order). See details below. BEWARE: This method is meant to serialise data from trusted user input, like from your own database server or clients under your control, it could be dangerous to allow untrusted users to pass <a href="JSON.html"><code>JSON</code></a> sources into it.</p>
</li><li>
<p>Argument <code>opts</code>, if given, contains a Hash of options for the parsing. See <a href="#module-JSON-label-Parsing+Options">Parsing Options</a>. The default options can be changed via method <a href="JSON.html#attribute-c-load_default_options"><code>JSON.load_default_options=</code></a>.</p>
</li></ul>
<hr>

<p>When no <code>proc</code> is given, modifies <code>source</code> as above and returns the result of <code>parse(source, opts)</code>;  see <a href="JSON.html#method-i-parse"><code>parse</code></a>.</p>

<p>Source for following examples:</p>

<pre class="ruby"><span class="ruby-identifier">source</span> = <span class="ruby-identifier">&lt;&lt;-EOT</span>
<span class="ruby-value">{
&quot;name&quot;: &quot;Dave&quot;,
  &quot;age&quot; :40,
  &quot;hats&quot;: [
    &quot;Cattleman&#39;s&quot;,
    &quot;Panama&quot;,
    &quot;Tophat&quot;
  ]
}
</span><span class="ruby-identifier">EOT</span>
</pre>

<p>Load a String:</p>

<pre class="ruby"><span class="ruby-identifier">ruby</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">load</span>(<span class="ruby-identifier">source</span>)
<span class="ruby-identifier">ruby</span> <span class="ruby-comment"># =&gt; {&quot;name&quot;=&gt;&quot;Dave&quot;, &quot;age&quot;=&gt;40, &quot;hats&quot;=&gt;[&quot;Cattleman&#39;s&quot;, &quot;Panama&quot;, &quot;Tophat&quot;]}</span>
</pre>

<p>Load an IO object:</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;stringio&#39;</span>
<span class="ruby-identifier">object</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">load</span>(<span class="ruby-constant">StringIO</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">source</span>))
<span class="ruby-identifier">object</span> <span class="ruby-comment"># =&gt; {&quot;name&quot;=&gt;&quot;Dave&quot;, &quot;age&quot;=&gt;40, &quot;hats&quot;=&gt;[&quot;Cattleman&#39;s&quot;, &quot;Panama&quot;, &quot;Tophat&quot;]}</span>
</pre>

<p>Load a File object:</p>

<pre class="ruby"><span class="ruby-identifier">path</span> = <span class="ruby-string">&#39;t.json&#39;</span>
<span class="ruby-constant">File</span>.<span class="ruby-identifier">write</span>(<span class="ruby-identifier">path</span>, <span class="ruby-identifier">source</span>)
<span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span>(<span class="ruby-identifier">path</span>) <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">file</span><span class="ruby-operator">|</span>
  <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">load</span>(<span class="ruby-identifier">file</span>)
<span class="ruby-keyword">end</span> <span class="ruby-comment"># =&gt; {&quot;name&quot;=&gt;&quot;Dave&quot;, &quot;age&quot;=&gt;40, &quot;hats&quot;=&gt;[&quot;Cattleman&#39;s&quot;, &quot;Panama&quot;, &quot;Tophat&quot;]}</span>
</pre>
<hr>

<p>When <code>proc</code> is given:</p>
<ul><li>
<p>Modifies <code>source</code> as above.</p>
</li><li>
<p>Gets the <code>result</code> from calling <code>parse(source, opts)</code>.</p>
</li><li>
<p>Recursively calls <code>proc(result)</code>.</p>
</li><li>
<p>Returns the final result.</p>
</li></ul>

<p>Example:</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;json&#39;</span>

<span class="ruby-comment"># Some classes for the example.</span>
<span class="ruby-keyword">class</span> <span class="ruby-constant">Base</span>
  <span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">initialize</span>(<span class="ruby-identifier">attributes</span>)
    <span class="ruby-ivar">@attributes</span> = <span class="ruby-identifier">attributes</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">class</span> <span class="ruby-constant">User</span>    <span class="ruby-operator">&lt;</span> <span class="ruby-constant">Base</span>; <span class="ruby-keyword">end</span>
<span class="ruby-keyword">class</span> <span class="ruby-constant">Account</span> <span class="ruby-operator">&lt;</span> <span class="ruby-constant">Base</span>; <span class="ruby-keyword">end</span>
<span class="ruby-keyword">class</span> <span class="ruby-constant">Admin</span>   <span class="ruby-operator">&lt;</span> <span class="ruby-constant">Base</span>; <span class="ruby-keyword">end</span>
<span class="ruby-comment"># The JSON source.</span>
<span class="ruby-identifier">json</span> = <span class="ruby-identifier">&lt;&lt;-EOF</span>
<span class="ruby-value">{
  &quot;users&quot;: [
      {&quot;type&quot;: &quot;User&quot;, &quot;username&quot;: &quot;jane&quot;, &quot;email&quot;: &quot;jane@example.com&quot;},
      {&quot;type&quot;: &quot;User&quot;, &quot;username&quot;: &quot;john&quot;, &quot;email&quot;: &quot;john@example.com&quot;}
  ],
  &quot;accounts&quot;: [
      {&quot;account&quot;: {&quot;type&quot;: &quot;Account&quot;, &quot;paid&quot;: true, &quot;account_id&quot;: &quot;1234&quot;}},
      {&quot;account&quot;: {&quot;type&quot;: &quot;Account&quot;, &quot;paid&quot;: false, &quot;account_id&quot;: &quot;1235&quot;}}
  ],
  &quot;admins&quot;: {&quot;type&quot;: &quot;Admin&quot;, &quot;password&quot;: &quot;0wn3d&quot;}
}
</span><span class="ruby-identifier">EOF</span>
<span class="ruby-comment"># Deserializer method.</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">deserialize_obj</span>(<span class="ruby-identifier">obj</span>, <span class="ruby-identifier">safe_types</span> = <span class="ruby-node">%w(User Account Admin)</span>)
  <span class="ruby-identifier">type</span> = <span class="ruby-identifier">obj</span>.<span class="ruby-identifier">is_a?</span>(<span class="ruby-constant">Hash</span>) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">obj</span>[<span class="ruby-string">&quot;type&quot;</span>]
  <span class="ruby-identifier">safe_types</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">type</span>) <span class="ruby-operator">?</span> <span class="ruby-constant">Object</span>.<span class="ruby-identifier">const_get</span>(<span class="ruby-identifier">type</span>).<span class="ruby-identifier">new</span>(<span class="ruby-identifier">obj</span>) <span class="ruby-operator">:</span> <span class="ruby-identifier">obj</span>
<span class="ruby-keyword">end</span>
<span class="ruby-comment"># Call to JSON.load</span>
<span class="ruby-identifier">ruby</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">load</span>(<span class="ruby-identifier">json</span>, <span class="ruby-identifier">proc</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">obj</span><span class="ruby-operator">|</span>
  <span class="ruby-keyword">case</span> <span class="ruby-identifier">obj</span>
  <span class="ruby-keyword">when</span> <span class="ruby-constant">Hash</span>
    <span class="ruby-identifier">obj</span>.<span class="ruby-identifier">each</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">k</span>, <span class="ruby-identifier">v</span><span class="ruby-operator">|</span> <span class="ruby-identifier">obj</span>[<span class="ruby-identifier">k</span>] = <span class="ruby-identifier">deserialize_obj</span> <span class="ruby-identifier">v</span> }
  <span class="ruby-keyword">when</span> <span class="ruby-constant">Array</span>
    <span class="ruby-identifier">obj</span>.<span class="ruby-identifier">map!</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">v</span><span class="ruby-operator">|</span> <span class="ruby-identifier">deserialize_obj</span> <span class="ruby-identifier">v</span> }
  <span class="ruby-keyword">end</span>
})
<span class="ruby-identifier">pp</span> <span class="ruby-identifier">ruby</span>
</pre>

<p>Output:</p>

<pre>{&quot;users&quot;=&gt;
   [#&lt;User:0x00000000064c4c98
     @attributes=
       {&quot;type&quot;=&gt;&quot;User&quot;, &quot;username&quot;=&gt;&quot;jane&quot;, &quot;email&quot;=&gt;&quot;jane@example.com&quot;}&gt;,
     #&lt;User:0x00000000064c4bd0
     @attributes=
       {&quot;type&quot;=&gt;&quot;User&quot;, &quot;username&quot;=&gt;&quot;john&quot;, &quot;email&quot;=&gt;&quot;john@example.com&quot;}&gt;],
 &quot;accounts&quot;=&gt;
   [{&quot;account&quot;=&gt;
       #&lt;Account:0x00000000064c4928
       @attributes={&quot;type&quot;=&gt;&quot;Account&quot;, &quot;paid&quot;=&gt;true, &quot;account_id&quot;=&gt;&quot;1234&quot;}&gt;},
    {&quot;account&quot;=&gt;
       #&lt;Account:0x00000000064c4680
       @attributes={&quot;type&quot;=&gt;&quot;Account&quot;, &quot;paid&quot;=&gt;false, &quot;account_id&quot;=&gt;&quot;1235&quot;}&gt;}],
 &quot;admins&quot;=&gt;
   #&lt;Admin:0x00000000064c41f8
   @attributes={&quot;type&quot;=&gt;&quot;Admin&quot;, &quot;password&quot;=&gt;&quot;0wn3d&quot;}&gt;}</pre>

                              <div class="method-source-code" id="load-source">
            <pre><span class="ruby-comment"># File json/lib/json/common.rb, line 537</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">load</span>(<span class="ruby-identifier">source</span>, <span class="ruby-identifier">proc</span> = <span class="ruby-keyword">nil</span>, <span class="ruby-identifier">options</span> = {})
  <span class="ruby-identifier">opts</span> = <span class="ruby-identifier">load_default_options</span>.<span class="ruby-identifier">merge</span> <span class="ruby-identifier">options</span>
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">source</span>.<span class="ruby-identifier">respond_to?</span> <span class="ruby-value">:to_str</span>
    <span class="ruby-identifier">source</span> = <span class="ruby-identifier">source</span>.<span class="ruby-identifier">to_str</span>
  <span class="ruby-keyword">elsif</span> <span class="ruby-identifier">source</span>.<span class="ruby-identifier">respond_to?</span> <span class="ruby-value">:to_io</span>
    <span class="ruby-identifier">source</span> = <span class="ruby-identifier">source</span>.<span class="ruby-identifier">to_io</span>.<span class="ruby-identifier">read</span>
  <span class="ruby-keyword">elsif</span> <span class="ruby-identifier">source</span>.<span class="ruby-identifier">respond_to?</span>(<span class="ruby-value">:read</span>)
    <span class="ruby-identifier">source</span> = <span class="ruby-identifier">source</span>.<span class="ruby-identifier">read</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">opts</span>[<span class="ruby-value">:allow_blank</span>] <span class="ruby-operator">&amp;&amp;</span> (<span class="ruby-identifier">source</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">source</span>.<span class="ruby-identifier">empty?</span>)
    <span class="ruby-identifier">source</span> = <span class="ruby-string">&#39;null&#39;</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-identifier">result</span> = <span class="ruby-identifier">parse</span>(<span class="ruby-identifier">source</span>, <span class="ruby-identifier">opts</span>)
  <span class="ruby-identifier">recurse_proc</span>(<span class="ruby-identifier">result</span>, <span class="ruby-operator">&amp;</span><span class="ruby-identifier">proc</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">proc</span>
  <span class="ruby-identifier">result</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>

                            <div class="aliases">
                              Also aliased as: <a href="JSON.html#method-i-restore">restore</a>
                            </div>

                          </div>

                  <div id="method-i-load_file" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          load_file(path, opts={}) &rarr; object
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Calls:</p>

<pre class="ruby"><span class="ruby-identifier">parse</span>(<span class="ruby-constant">File</span>.<span class="ruby-identifier">read</span>(<span class="ruby-identifier">path</span>), <span class="ruby-identifier">opts</span>)
</pre>

<p>See method <a href="JSON.html#method-i-parse"><code>parse</code></a>.</p>

                              <div class="method-source-code" id="load_file-source">
            <pre><span class="ruby-comment"># File json/lib/json/common.rb, line 245</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">load_file</span>(<span class="ruby-identifier">filespec</span>, <span class="ruby-identifier">opts</span> = {})
  <span class="ruby-identifier">parse</span>(<span class="ruby-constant">File</span>.<span class="ruby-identifier">read</span>(<span class="ruby-identifier">filespec</span>), <span class="ruby-identifier">opts</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-load_file-21" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          load_file!(path, opts = {})
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Calls:</p>

<pre class="ruby"><span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse!</span>(<span class="ruby-constant">File</span>.<span class="ruby-identifier">read</span>(<span class="ruby-identifier">path</span>, <span class="ruby-identifier">opts</span>))
</pre>

<p>See method <a href="JSON.html#method-i-parse-21"><code>parse!</code></a></p>

                              <div class="method-source-code" id="load_file-21-source">
            <pre><span class="ruby-comment"># File json/lib/json/common.rb, line 256</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">load_file!</span>(<span class="ruby-identifier">filespec</span>, <span class="ruby-identifier">opts</span> = {})
  <span class="ruby-identifier">parse!</span>(<span class="ruby-constant">File</span>.<span class="ruby-identifier">read</span>(<span class="ruby-identifier">filespec</span>), <span class="ruby-identifier">opts</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-parse" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          parse(source, opts) &rarr; object
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Returns the Ruby objects created by parsing the given <code>source</code>.</p>

<p>Argument <code>source</code> contains the String to be parsed.</p>

<p>Argument <code>opts</code>, if given, contains a Hash of options for the parsing. See <a href="#module-JSON-label-Parsing+Options">Parsing Options</a>.</p>
<hr>

<p>When <code>source</code> is a JSON array, returns a Ruby Array:</p>

<pre class="ruby"><span class="ruby-identifier">source</span> = <span class="ruby-string">&#39;[&quot;foo&quot;, 1.0, true, false, null]&#39;</span>
<span class="ruby-identifier">ruby</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">source</span>)
<span class="ruby-identifier">ruby</span> <span class="ruby-comment"># =&gt; [&quot;foo&quot;, 1.0, true, false, nil]</span>
<span class="ruby-identifier">ruby</span>.<span class="ruby-identifier">class</span> <span class="ruby-comment"># =&gt; Array</span>
</pre>

<p>When <code>source</code> is a JSON object, returns a Ruby Hash:</p>

<pre class="ruby"><span class="ruby-identifier">source</span> = <span class="ruby-string">&#39;{&quot;a&quot;: &quot;foo&quot;, &quot;b&quot;: 1.0, &quot;c&quot;: true, &quot;d&quot;: false, &quot;e&quot;: null}&#39;</span>
<span class="ruby-identifier">ruby</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">source</span>)
<span class="ruby-identifier">ruby</span> <span class="ruby-comment"># =&gt; {&quot;a&quot;=&gt;&quot;foo&quot;, &quot;b&quot;=&gt;1.0, &quot;c&quot;=&gt;true, &quot;d&quot;=&gt;false, &quot;e&quot;=&gt;nil}</span>
<span class="ruby-identifier">ruby</span>.<span class="ruby-identifier">class</span> <span class="ruby-comment"># =&gt; Hash</span>
</pre>

<p>For examples of parsing for all JSON data types, see <a href="#module-JSON-label-Parsing+JSON">Parsing JSON</a>.</p>

<p>Parses nested <a href="JSON.html"><code>JSON</code></a> objects:</p>

<pre class="ruby"><span class="ruby-identifier">source</span> = <span class="ruby-identifier">&lt;&lt;-EOT</span>
<span class="ruby-value">{
&quot;name&quot;: &quot;Dave&quot;,
  &quot;age&quot; :40,
  &quot;hats&quot;: [
    &quot;Cattleman&#39;s&quot;,
    &quot;Panama&quot;,
    &quot;Tophat&quot;
  ]
}
</span><span class="ruby-identifier">EOT</span>
<span class="ruby-identifier">ruby</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-identifier">source</span>)
<span class="ruby-identifier">ruby</span> <span class="ruby-comment"># =&gt; {&quot;name&quot;=&gt;&quot;Dave&quot;, &quot;age&quot;=&gt;40, &quot;hats&quot;=&gt;[&quot;Cattleman&#39;s&quot;, &quot;Panama&quot;, &quot;Tophat&quot;]}</span>
</pre>
<hr>

<p>Raises an exception if <code>source</code> is not valid JSON:</p>

<pre class="ruby"><span class="ruby-comment"># Raises JSON::ParserError (783: unexpected token at &#39;&#39;):</span>
<span class="ruby-constant">JSON</span>.<span class="ruby-identifier">parse</span>(<span class="ruby-string">&#39;&#39;</span>)
</pre>

                              <div class="method-source-code" id="parse-source">
            <pre><span class="ruby-comment"># File json/lib/json/common.rb, line 215</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">parse</span>(<span class="ruby-identifier">source</span>, <span class="ruby-identifier">opts</span> = {})
  <span class="ruby-constant">Parser</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">source</span>, <span class="ruby-operator">**</span>(<span class="ruby-identifier">opts</span><span class="ruby-operator">||</span>{})).<span class="ruby-identifier">parse</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-parse-21" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          parse!(source, opts) &rarr; object
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Calls</p>

<pre class="ruby"><span class="ruby-identifier">parse</span>(<span class="ruby-identifier">source</span>, <span class="ruby-identifier">opts</span>)
</pre>

<p>with <code>source</code> and possibly modified <code>opts</code>.</p>

<p>Differences from <a href="JSON.html#method-i-parse"><code>JSON.parse</code></a>:</p>
<ul><li>
<p>Option <code>max_nesting</code>, if not provided, defaults to <code>false</code>, which disables checking for nesting depth.</p>
</li><li>
<p>Option <code>allow_nan</code>, if not provided, defaults to <code>true</code>.</p>
</li></ul>

                              <div class="method-source-code" id="parse-21-source">
            <pre><span class="ruby-comment"># File json/lib/json/common.rb, line 230</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">parse!</span>(<span class="ruby-identifier">source</span>, <span class="ruby-identifier">opts</span> = {})
  <span class="ruby-identifier">opts</span> = {
    <span class="ruby-value">:max_nesting</span>  <span class="ruby-operator">=&gt;</span> <span class="ruby-keyword">false</span>,
    <span class="ruby-value">:allow_nan</span>    <span class="ruby-operator">=&gt;</span> <span class="ruby-keyword">true</span>
  }.<span class="ruby-identifier">merge</span>(<span class="ruby-identifier">opts</span>)
  <span class="ruby-constant">Parser</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">source</span>, <span class="ruby-operator">**</span>(<span class="ruby-identifier">opts</span><span class="ruby-operator">||</span>{})).<span class="ruby-identifier">parse</span>
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                  <div id="method-i-pretty_generate" class="method-detail ">
                      <div class="method-heading">
                        <span class="method-callseq">
                          pretty_generate(obj, opts = nil) &rarr; new_string
                              </span>
                              <span class="method-click-advice">click to toggle source</span>
                            </div>

                            <div class="method-description">
                              <p>Arguments <code>obj</code> and <code>opts</code> here are the same as arguments <code>obj</code> and <code>opts</code> in <a href="JSON.html#method-i-generate"><code>JSON.generate</code></a>.</p>

<p>Default options are:</p>

<pre class="ruby">{
  <span class="ruby-value">indent:</span> <span class="ruby-string">&#39;  &#39;</span>,   <span class="ruby-comment"># Two spaces</span>
  <span class="ruby-value">space:</span> <span class="ruby-string">&#39; &#39;</span>,     <span class="ruby-comment"># One space</span>
  <span class="ruby-value">array_nl:</span> <span class="ruby-string">&quot;\n&quot;</span>, <span class="ruby-comment"># Newline</span>
  <span class="ruby-value">object_nl:</span> <span class="ruby-string">&quot;\n&quot;</span> <span class="ruby-comment"># Newline</span>
}
</pre>

<p>Example:</p>

<pre class="ruby"><span class="ruby-identifier">obj</span> = {<span class="ruby-value">foo:</span> [<span class="ruby-value">:bar</span>, <span class="ruby-value">:baz</span>], <span class="ruby-value">bat:</span> {<span class="ruby-value">bam:</span> <span class="ruby-value">0</span>, <span class="ruby-value">bad:</span> <span class="ruby-value">1</span>}}
<span class="ruby-identifier">json</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">pretty_generate</span>(<span class="ruby-identifier">obj</span>)
<span class="ruby-identifier">puts</span> <span class="ruby-identifier">json</span>
</pre>

<p>Output:</p>

<pre class="ruby">{
  <span class="ruby-value">&quot;foo&quot;:</span> [
    <span class="ruby-string">&quot;bar&quot;</span>,
    <span class="ruby-string">&quot;baz&quot;</span>
  ],
  <span class="ruby-value">&quot;bat&quot;:</span> {
    <span class="ruby-value">&quot;bam&quot;:</span> <span class="ruby-value">0</span>,
    <span class="ruby-value">&quot;bad&quot;:</span> <span class="ruby-value">1</span>
  }
}
</pre>

                              <div class="method-source-code" id="pretty_generate-source">
            <pre><span class="ruby-comment"># File json/lib/json/common.rb, line 370</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier ruby-title">pretty_generate</span>(<span class="ruby-identifier">obj</span>, <span class="ruby-identifier">opts</span> = <span class="ruby-keyword">nil</span>)
  <span class="ruby-keyword">if</span> <span class="ruby-constant">State</span> <span class="ruby-operator">===</span> <span class="ruby-identifier">opts</span>
    <span class="ruby-identifier">state</span>, <span class="ruby-identifier">opts</span> = <span class="ruby-identifier">opts</span>, <span class="ruby-keyword">nil</span>
  <span class="ruby-keyword">else</span>
    <span class="ruby-identifier">state</span> = <span class="ruby-constant">JSON</span>.<span class="ruby-identifier">create_pretty_state</span>
  <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">if</span> <span class="ruby-identifier">opts</span>
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">opts</span>.<span class="ruby-identifier">respond_to?</span> <span class="ruby-value">:to_hash</span>
      <span class="ruby-identifier">opts</span> = <span class="ruby-identifier">opts</span>.<span class="ruby-identifier">to_hash</span>
    <span class="ruby-keyword">elsif</span> <span class="ruby-identifier">opts</span>.<span class="ruby-identifier">respond_to?</span> <span class="ruby-value">:to_h</span>
      <span class="ruby-identifier">opts</span> = <span class="ruby-identifier">opts</span>.<span class="ruby-identifier">to_h</span>
    <span class="ruby-keyword">else</span>
      <span class="ruby-identifier">raise</span> <span class="ruby-constant">TypeError</span>, <span class="ruby-node">&quot;can&#39;t convert #{opts.class} into Hash&quot;</span>
    <span class="ruby-keyword">end</span>
    <span class="ruby-identifier">state</span>.<span class="ruby-identifier">configure</span>(<span class="ruby-identifier">opts</span>)
  <span class="ruby-keyword">end</span>
  <span class="ruby-identifier">state</span>.<span class="ruby-identifier">generate</span>(<span class="ruby-identifier">obj</span>)
<span class="ruby-keyword">end</span></pre>
                              </div>
                            </div>


                          </div>

                          </section>

                <section id="private-instance-5Buntitled-5D-method-details" class="method-section">
                <header>
                <h3>Private Instance Methods</h3>
                </header>

                  <div id="method-i-restore" class="method-detail method-alias">
                            <div class="method-heading">
                              <span class="method-name">restore</span><span
                                class="method-args">(source, proc = nil, options = {})</span>
                            </div>

                            <div class="method-description">
                              

                            </div>


                            <div class="aliases">
                              Alias for: <a href="JSON.html#method-i-load">load</a>
                            </div>
                          </div>

                          </section>

              </section>
              </main>



            </div>  <!--  class='wrapper hdiv' -->


<footer id="validator-badges" role="contentinfo">
<p><a href="https://validator.w3.org/check/referer">Validate</a></p>
<p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.4.0.</p>
<p>Based on <a href="https://github.com/ged/darkfish/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.</p>

  
    <p><p><a href="https://ruby-doc.org">Ruby-doc.org</a> is a service of <a href="https://jamesbritt.com">James Britt</a> and <a href="https://neurogami.com">Neurogami</a>, purveyors of fine <a href='https://jamesbritt.bandcamp.com/'>dance noise</a></p>
</p>
  
  </footer>

<script type="text/javascript">


  let ads  = $("#carbonads-container").children().detach();


  function swapMode() {
    var cookieName = 'darkmode';
    var cssDarkmode = Cookies.get(cookieName);
    console.log("***** swapMode! " + cssDarkmode + " *****");


    if (cssDarkmode == "true") {
      console.log("We have dark mode, set the css to light ...");
      $('#rdoccss').attr("href", defaultModeCssHref);
      $('#cssSelect').text("Dark mode");
      cssDarkmode = "false";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    } else {
      console.log("We not have dark mode, set the css to dark ...");
      $('#rdoccss').attr("href", darkModeCsseHref);
      $('#cssSelect').text("Light mode");
      cssDarkmode = "true";
      console.log("swapMode! Now set cookie to " + cssDarkmode);
      Cookies.set(cookieName, cssDarkmode);

    }

    console.log("  --------------- ");
  }


const vueCssApp = new Vue({
el: '#menubar',
data: {
isDark: false
},
methods: {
toggleClass: function(event){
this.isDark = !this.isDark;
}
}
})

const vueApp = new Vue({
el: '#vapp',
data: { 
isOpen: true
},

mounted() {
this.handleResize();
this.manage_mob_classes();
window.addEventListener('resize', this.handleResize)
//this.isOpen !=  (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},
destroyed() {
window.removeEventListener('resize', this.handleResize)
},
created() {
//manage_mob_classes();
},

methods : {
isMobile() {
  return (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent));
},

  handleResize() {
    if (!this.isMobile()) {
      this.isOpen = window.innerWidth > 800;
    }
  },

  manage_mob_classes() {
    if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)) {
      $("nav").addClass("mob_nav");
      $("main").addClass("mob_main");
      $("#extraz").addClass("mob_extraz");
      $("#carbonads-container").addClass("mob_carbonads-container");
      this.isOpen  = false;
    } else {
      $("nav").removeClass("mob_nav") 
        $("main").removeClass("mob_main");
      $("#extraz").removeClass("mob_extraz");
      $("#carbonads-container").removeClass("mob_carbonads-container");
      this.isOpen  = true;
    }
  },

  toggleNav() {
    this.isOpen =! this.isOpen ;
    // alert("Toggle nav!");
    console.log("toggleNav() click: " + this.isOpen );
  }
}
})

$("#carbonads-container").append(ads);


$(function() {

    var darkmode = Cookies.get("darkmode");
    console.log("Document ready: " + darkmode);

    if ( darkmode  == "true" ) {
      $('#cssSelect').text("Light mode");
    } else {
      $('#cssSelect').text("Dark mode");
     }

    $('body').css('display','block');
    });

</script>

    
  </body> 
</html>

